SQL connection string for microsoft access 2010 .accdb

后端 未结 4 1677
-上瘾入骨i
-上瘾入骨i 2020-12-03 15:46

I am doing a simple login form using winforms and access 2010 database (.accdb) in C#.

I have the following code and it seems that the connection string is wrong. I

相关标签:
4条回答
  • comm.CommandText = "SELECT HAHA(*) FROM password";
    

    It´s wrong.

    "SELECT password FROM HAHA"

    0 讨论(0)
  • 2020-12-03 16:27

    Edit: as pointed out, for access OleDbConnection should be used, not SqlConnection...

    you can use a much more compact way and also be sure connection is closed and disposed in any possible case even when exceptions are thrown, by using the using statements:

    your query text was also, probably wrong as others have suggested...

    using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BC207\test.accdb"))
    using (var comm = conn.CreateCommand())
    {
        comm.CommandText = "SELECT password FROM HAHA";
        comm.CommandType = CommandType.Text;
    
        conn.Open();
    
        var returnValue = comm.ExecuteScalar();
    
        MessageBox.Show(returnValue.ToString());
    }
    

    Edit: are you sure the table HAHA only contains one row? Because the ExecuteScalar returns only one value, if you want to get 1 column but from many records you could use a DataReader or a DataSet...

    0 讨论(0)
  • 2020-12-03 16:30

    Your SQL statement should be,

    SELECT * from HAHA
    

    OR

     SELECT [Password] From HAHA
    

    EDIT:

    You should change the ConnectionString.

    0 讨论(0)
  • 2020-12-03 16:34

    For Acces databases (.mdb, .accdb, etc...), you want to use OleDbConnection, not SqlConnection (SQL Server), like this:

    conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BC207\test.accdb")
    
    0 讨论(0)
提交回复
热议问题