Hey guys i've got this block of code i'm trying to just connect to the DB, pull out a table and traverse the columns.
String dbpath =
Path.Combine(Application.StartupPath,
"Database1.mdf");
String conn = @"Server=.\SQLExpress;AttachDbFilename=" + dbpath + ";Database=database1;Trusted_Connection=Yes;";
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM Student", con);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
MessageBox.Show("Retrieved ID:" + reader["ID"] + ", Name: " + reader["Name"]);
}
I have no idea what's wrong.
I keep getting thrown exceptiosn about cannot attach a DB.
What i'm trying to do is connect to a SQL 2005 .mdb file locally.
I've setup my SQL configuration to allow remote connections, it's all set.
I have created the DB already called Database1.mdf, with one Student table with ID, Name, LastName.
Also one more question, how do i find out about the Database name that i specify int he connection string normally, or is that an arbitrary name i make up that's unique?
And, how does one set the User and Password of a .mdf file?
Thanks for your help guys.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.