简体   繁体   中英

C#.NET connection string cannot connect

I'm using Visual C# Express 2008 and created a database and dataset by going to Add->New Data Source. I'm trying to add a record to it but cannot connect using the data string provided in the wizard. Please help. Here is my code

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=|DataDirectory|\\myLife.sdf;Password=*******");

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT profiles (profile, file) VALUES ('here', 'here')";
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();

由于您使用的是SQL Compact Edition数据库文件(.sdf),因此应使用Sql Connection代替SqlConnection,如本其他文章中所述

Normally, you need to specify the username and the password in the same connection string. I know that you can use your windows credentials to connect by using integrated security=true in the connection string, but I think that in your case, you probably want to use Data Source=|DataDirectory|\\\\myLife.sdf;User id=Bob;Password=*******

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM