Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Set AttachDbFilename in connection string

using System; using System.Data; using System.Data.SqlClient;   class Program   {     static void Main(string[] args)     {       SqlConnection thisConnection = new SqlConnection(                 @"Data Source=.\SQLEXPRESS;" +                 @"AttachDbFilename='NORTHWND.MDF';" +                 @"Integrated Security=True;Connect Timeout=30;User Instance=true");       thisConnection.Open();       SqlCommand thisCommand = thisConnection.CreateCommand();       thisCommand.CommandText = "SELECT CustomerID, CompanyName from Customers";       SqlDataReader thisReader = thisCommand.ExecuteReader();       while (thisReader.Read())       {         Console.WriteLine("\t{0}\t{1}",         thisReader["CustomerID"], thisReader["CompanyName"]);       }       thisReader.Close();       thisConnection.Close();     }   }