连接到本地数据库时出错 con.open()
Error connecting to Local Database con.open()
我安装了示例数据库 Northwind
并将其附加到 C# 项目
我正在尝试连接到数据库并检索数据库中 table 的列名,但在尝试打开连接时出现错误,这是我的代码用于这样做:
public void connectToDB()
{
Dictionary<object, object> colns = new Dictionary<object, object>();
List<string> colnNames = new List<string>();
con = new SqlConnection("Data Source = .NorthwindDB.mdf; Integrated Security=True");
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM Products";
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables["Products"];
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dr.Table.Columns)
{
colnNames.Add(dc.ColumnName.ToString());
}
}
foreach(string key in colnNames)
{
Console.WriteLine(key.ToString());
}
Console.ReadKey();
}
我收到以下错误:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
另一件可能有帮助的事情是,我注意到每当我单击 start
按钮执行代码时,数据连接上显示的绿色插头 NorthwindDB.mdf
会变成红色 x。
您的连接字符串不正确,应该是这样的:
con = new SqlConnection("Data Source=(local);" +
"Initial Catalog=NorthwindDB.mdf;Integrated Security=True");
// Or Data Source=.\sqlexpress;
您可以在此处阅读有关连接字符串的更多信息:Database Connectionstrings
。
我安装了示例数据库 Northwind
并将其附加到 C# 项目
我正在尝试连接到数据库并检索数据库中 table 的列名,但在尝试打开连接时出现错误,这是我的代码用于这样做:
public void connectToDB()
{
Dictionary<object, object> colns = new Dictionary<object, object>();
List<string> colnNames = new List<string>();
con = new SqlConnection("Data Source = .NorthwindDB.mdf; Integrated Security=True");
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM Products";
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables["Products"];
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dr.Table.Columns)
{
colnNames.Add(dc.ColumnName.ToString());
}
}
foreach(string key in colnNames)
{
Console.WriteLine(key.ToString());
}
Console.ReadKey();
}
我收到以下错误:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
另一件可能有帮助的事情是,我注意到每当我单击 start
按钮执行代码时,数据连接上显示的绿色插头 NorthwindDB.mdf
会变成红色 x。
您的连接字符串不正确,应该是这样的:
con = new SqlConnection("Data Source=(local);" +
"Initial Catalog=NorthwindDB.mdf;Integrated Security=True");
// Or Data Source=.\sqlexpress;
您可以在此处阅读有关连接字符串的更多信息:Database Connectionstrings
。