在 Visual Studio 2010 上,我有一个试图连接的数据库,但是当我尝试这样做时:
db.Open();
它启动了这个错误:
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: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)
我试图按照这个链接所说的去做,但我仍然犯同样的错误。
关于发生了什么的任何想法?
编辑:防火墙已关闭。
连接字符串: MySQLProject.Properties.Settings.Default.dbConnectionString = "Data Source=|DataDirectory|\db.sdf"
服务器已启动:
编辑2:
这是失败的代码:
public void FillData()
{
// 1 step. Open connection
// SqlConnection c = new SqlConnection("Data Source=" +db.Connection.DataSource);
SqlConnection c = new SqlConnection(MySQLProject.Properties.Settings.Default.dbConnectionString);
try
{
c.Open();
// 2 step. Create new DataAdapter
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM USER", c))
{
// 3 step. Use DataAdapter to fill table
DataTable t = new DataTable();
a.Fill(t);
// 4 step. Render data on the DataGridView
dataGridViewUsers.DataSource = t;
}
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}
}
编辑 nº 1000:
好的,我使用了这个连接字符串:
string con2 = @"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|db.sdf;Database=db;Trusted_Connection=Yes;";
然后它说:
:____(
好的,现在我知道这.sdf
是针对 CE Sql 语句的。但是,我不能创建.mdf
,不知道为什么...我应该更改为 CE Sql 语句吗?