我是C#的新手,我需要连接到.sdf数据库,我使用以下连接,但一直得到异常,我不知道为什么
bool stat = true;
string connectionString = "Data Source=|DataDirectory|'\'CarsDB.sdf;Initial Catalog=TestDB;Integrated Security=true;";
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
textBox2.Text = "true";
}
catch (System.Exception)
{
stat = false;
textBox2.Text = "false";
}
例外是:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (provider:Named Pipes Provider,错误:40 - 无法打开与SQL Server的连接)。
答案 0 :(得分:1)
这是代码:
bool stat
string connectionString = "Data Source=|DataDirectory|'\'CarsDB.sdf;Initial Catalog=TestDB;Integrated Security=true;";
try
{
conn = new SqlCeConnection(connectionString );
conn.Open();
textBox2.Text = "true";
/* SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";
cmd.ExecuteNonQuery();*/
}
catch (System.Exception)
{
stat = false;
textBox2.Text = "false";
}