【发布时间】:2016-03-14 19:17:29
【问题描述】:
我已将 SQL Server .mdf 数据库文件添加到我的 C# 应用程序,但是当我尝试使用此代码连接时,程序会导致连接错误。
代码:
DataSet data;
string con = "Data Source=dbinterno.mdf;";
string queryString = "Select * FROM Dati";
try
{
using (SqlConnection connection = new SqlConnection(con))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
data = new DataSet();
adapter.Fill(data);
MessageBox.Show(data.ToString());
connection.Close();
}
}
catch
{
MessageBox.Show("\n Problemi di connessione al database");
}
错误是:
【问题讨论】:
-
我们需要更多信息......初学者有什么错误?
-
没有明确的信息。我们不知道是什么问题。
-
@trousyt 我有插入错误图片
-
您是否尝试使用绝对 mdf 文件路径而不是相对路径?
-
如果这个
string con = "Data Source=dbinterno.mdf;";是你所有的连接字符串,那么它还没有完成。连接字符串应该是,例如,Server=.\SQLSERVER;Initial Catalog=dbinterno;Integrated Security=True等等...在 google 中搜索 mssql 服务器中的连接字符串...就像错误所说的the server not found...,因为它没有在连接字符串中定义。
标签: c#