C# C-无法使用SQLServer连接到我的本地数据库

C# C-无法使用SQLServer连接到我的本地数据库,c#,sql-server,connection,local,C#,Sql Server,Connection,Local,我是C新手,正在尝试将我的应用程序连接到DB,所以我尝试了以下方法: public ActionResult ConnexionBD(){ SqlConnection cnx; cnx = new SqlConnection("Data Source=C:\\Users\\Antoine\\Documents\\Visual Studio 2012\\Projects\\Application\\Application\\App_Data\\Database1.

我是C新手,正在尝试将我的应用程序连接到DB,所以我尝试了以下方法:

public ActionResult ConnexionBD(){

        SqlConnection cnx;
        cnx = new SqlConnection("Data Source=C:\\Users\\Antoine\\Documents\\Visual Studio 2012\\Projects\\Application\\Application\\App_Data\\Database1.sdf;Initial Catalog=tstado;Integrated Security=True;Pooling=False");
        cnx.Open();
        SqlCommand cmd;
        cmd = new SqlCommand();
        cmd.CommandText="INSERT INTO Objet VALUES(1,'F',"+DateTime.Now+",'Moi')";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = cnx;

        cmd = new SqlCommand();
        cmd.CommandText = "SELECT * FROM Objet";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = cnx;
        SqlDataReader r;
        r = cmd.ExecuteReader();
        String chaine="";
        while (r.Read())
        {
            string nom = (string)r["Nom"];
            string date = (string)r["Date"];
            string user = (string)r["User"];

            chaine+=nom+"\t"+date+"\t"+user+"\n";
        }
        cnx.Close();
        return View(chaine);
    }
它在第4行中断:cnx.Open。 该错误表示由于网络原因,网络无法接通或无法访问。 法语中的确切错误是:在SQL Server上连接表格的实例是最容易产生错误的。这是一个很容易进入的服务。Vérifiez que le nom de l'instance是否正确,SQL Server是否配置了autoriser les connexions distantes

英语翻译

发生与特定于网络或实例的错误时 正在建立与SQL Server的连接。找不到服务器,或者 无法访问。验证实例名称是否正确,以及 SQL Server配置为允许远程连接

我认为字符串连接可能是错误的,但我不确定


如果有人能帮我

在解决方案资源管理器中,您应该将Db文件的属性“复制到输出目录”标记为“始终复制”,或者根据需要将其标记为“更新”,以便将其复制到调试器有权访问的BIN文件夹中。在您的连接字符串中,您只需将Database1.sdf放入,因为您使用的是本地数据库.sdf,您需要引用Add reference>Extensions中的System.Data.SqlServerCe,并相应地更改代码,请参见下文

public ActionResult ConnexionBD()
    {
        SqlCeConnection cnx = new SqlCeConnection(@"Data Source=C:\Users\Antoine\Documents\Visual Studio 2012\Projects\Application\Application\App_Data\Database1.sdf");
        cnx.Open();
        SqlCeCommand cmd = new SqlCeCommand
        {
            CommandText = "INSERT INTO Objet VALUES(1,'F'," + DateTime.Now + ",'Moi'",
            CommandType = CommandType.Text,
            Connection = cnx
        };

        cmd = new SqlCeCommand {CommandText = "SELECT * FROM Objet", CommandType = CommandType.Text, Connection = cnx};
        SqlCeDataReader r = cmd.ExecuteReader();
        String chaine = "";
        while (r.Read())
        {
            string nom = (string)r["Nom"];
            string date = (string)r["Date"];
            string user = (string)r["User"];

            chaine += nom + "\t" + date + "\t" + user + "\n";
        }
        cnx.Close();
        return View(chaine);
    }

你能把断点放在哪里看看它断开了吗?我做了,我仍然有同样的错误。我可能会尝试IIS,但问题是关键字不受支持:“初始目录”。感谢您的帮助抱歉,请删除*.sdf之后的connectionstring元素,本地db实例不需要它们。我已经更新了上面的代码来反映这一点。没问题。要记住一件事:这只适用于Visual Studio 2012及以下版本。Visual 2013+已转向使用LocalDB。看这里:好吧,我还是用2012