C# 通过SqlConnection/SqlCeConnection连接到.sdf数据库时出现问题

C# 通过SqlConnection/SqlCeConnection连接到.sdf数据库时出现问题,c#,sql,sql-server,C#,Sql,Sql Server,我很难连接到.sdf(sql compact edition)数据库。为了验证用户名/密码,我可以先连接以提取行,但当我尝试通过SqlCeConnection/SqlCeCommand命令或尝试通过SqlClient/SqlCommand连接向数据库添加项时,我没有运气。我得到: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The ser

我很难连接到.sdf(sql compact edition)数据库。为了验证用户名/密码,我可以先连接以提取行,但当我尝试通过SqlCeConnection/SqlCeCommand命令或尝试通过SqlClient/SqlCommand连接向数据库添加项时,我没有运气。我得到:

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)
使用时:

private void button1_Click_1(object sender, EventArgs e)
{
    System.Data.SqlClient.SqlConnection sqlConnection1 =
    new System.Data.SqlClient.SqlConnection("Data Source=C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf");

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT [Employee Table] (SSN, FirstName) VALUES ('555-23-4322', 'Tim')";
    cmd.Connection = sqlConnection1;

    sqlConnection1.Open();
    cmd.ExecuteNonQuery();
    sqlConnection1.Close();
}
或者当我尝试时:

System.Data.SqlClient.SqlConnection sqlConnection1 =
            new System.Data.SqlClient.SqlConnection("Data Source=C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf");

            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "INSERT [Employee Table] (SSN, FirstName) VALUES ('555-23-4322', 'Tim')";
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();
            cmd.ExecuteNonQuery();
            sqlConnection1.Close();
什么都没有发生,直到我检查.sdf文件,发现没有添加任何内容,它才开始工作。我没有安装SQLServerCE,不过我确实安装了2008R2(我正在处理其他人的项目)


现在我不担心安全漏洞,我只是想成功地添加一个记录。任何帮助都将不胜感激,因为我已经花了大约三四个小时来做一些我认为大约需要20分钟的事情

现在您可能已经找到了解决方案,但如果您使用sdf文件,则应在程序集中添加对System.Data.SqlServerCe的引用,然后添加类似以下内容:

SqlCeConnection sqlConnection1= new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf";

System.Data.SqlServerCe.SqlCeCommand cmd = System.Data.SqlServerCe.SqlCeCommand;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT [Employee Table] (SSN, FirstName) VALUES ('555-23-4322', 'Tim')";
cmd.Connection = sqlConnection1;

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

您最喜欢使用sqlceconnection和sqlcecommand类。像这样:

        SqlCeConnection conn = new SqlCeConnection(connectionString);            
        SqlCeCommand cmd = conn.CreateCommand();
        conn.Open();

谢谢你为我指明了正确的方向-谢谢!