简体   繁体   中英

Connect to sql database

I'm trying to retrieve data from a database to a datagridview but i'm getting an exception,

The ConnectionString property has not been initialized.

using (conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\BOB\Documents\Visual Studio 2012\Projects\Login\Login\Student_Marks.mdf"";Integrated Security=True"));
{
     using (adap = new SqlDataAdapter("select * from tbl_Students_Marks", conn))
     {
          DataSet das = new DataSet();
          adap.Fill(das);
          dataGridView1.DataSource = ds.Tables[0];
     }
}

i see your code and found that you placed ";" and the end of your first using statement where you initiating conn

using (conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\BOB\Documents\Visual Studio 2012\Projects\Login\Login\Student_Marks.mdf"";Integrated Security=True"))//; this was the issue
{
     using (adap = new SqlDataAdapter("select * from tbl_Students_Marks", conn))
     {
          DataSet das = new DataSet();
          adap.Fill(das);
          dataGridView1.DataSource = das.Tables[0];//this must be das not ds
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM