Sunday 4 January 2015

C# Retrieving Values From A local Database

i assume you have already watched the previous tutorial

if you havent then check this out
http://icytrey.blogspot.com/2015/01/c-easiest-way-to-connect-to-local.html
and can connect to your local database without any problems

just copy and paste its not a crime



   System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();

            con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";
            con.Open();
            SqlCommand command = new SqlCommand("select * from Movies", con);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                int Id = reader.GetInt32(0);
                String name = reader.GetString(1);
                String categoruy= reader.GetString(2);
                listBox1.Items.Add(name);

            }

          
            con.Close();

2 comments: