Tuesday 3 April 2012

C# how to open and operate .mdf file.


C# how to open and operate .mdf file:


First things first, the MS-SQL server should be installed in your computer.


!Core: ConnectionString settting:


//bold content is the absolute/relative path for your mdf file! 


String connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\My Documents\\WindowsApplication1\\xxx.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";


//processing......................
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select .......", conn);
SqlDataReader sdr = cmd.ExecuteReader();
 while (sdr.Read())
 {
     //processing....
 }
sdr.Close();

1 comment: