I feel like an absolute idiot. I've been trying to connect to a database I created for a few hours now and I can't seem to get it to connect. Here is my code.
string chooseMoodCmbBx = moodCmbBx.SelectedIndex.ToString();
string source = "Data Source='E:\\Documents\\Database\\MyDatabase.sdf';" +
"Password='password';" +
"Persist Security Info=False;";
SqlConnection conn = new SqlConnection(source);
try
{
conn.Open();
MessageBox.Show("Succesfully Connected");
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
You have an SDF file, meaning that you connect to a Sql Compact not to Sql Server. You need to use the classes in the namespace System.Data.SqlServerCe
SqlCeConnection conn = new SqlCeConnection(source);
Also, I am not sure of this, but I think you don't need to have single quotes around values in the connection string
string source = "Data Source=E:\\Documents\\Database\\MyDatabase.sdf;" +
"Password=password;" +
"Persist Security Info=False;";
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.