从 C# 代码与 SQL Server 2014 Express 的连接不起作用,无法打开连接

Connection from C # code with SQL Server 2014 Express does not work , can not open the connection

我正在使用 C# 在 WPF 中创建客户端,我需要将其安装在客户端(平板电脑表面)上。也就是说,我安装了 SQL Server 2014 Express 版,只是为了获取实例,我从这里下载:

https://www.microsoft.com/en-US/download/details.aspx?id=42299

我明白了:

Express 64BIT\SQLEXPR_x64_ENU.exe

我安装了所有东西,当然我没有SQL服务器接口的管理,但是我不能创建数据库,这是我的错误:

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

这是我的连接字符串(从常量文件中获取):

Server=SQLEXPRESS;Database=master;Integrated security=true;

这是我的代码:

 public void CreateDatabase(String PID)
        {
            String connection = Constants.localServerConnectionSQL.LocalServerConnectionSQLName;
            SqlConnection Connection = new SqlConnection();
            Connection = new SqlConnection(connection);
            Connection.Open();

            string Path = Environment.GetEnvironmentVariable("LocalAppData") + @"\CDA\UserDatabase\" + PID.ToString();
            log.Info("DBCreationScripts: Path DB: " + Path.ToString());

            String str = "CREATE DATABASE [" + PID + "] ON PRIMARY " +
           "(NAME = MyDatabase_Data, " +
           "FILENAME = '" + Path + ".mdf', " +
           "SIZE = 5MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
           "LOG ON (NAME = [" + PID + "Log_Log], " +
           "FILENAME = '"+Path+"Log.ldf', " +
           "SIZE = 1MB, " +
           "MAXSIZE = 5MB, " +
           "FILEGROWTH = 10%)";

            log.Info("DBCreationScripts: Comando di creazione DB: " + str);


            try
            {
                SqlCommand myCommand = new SqlCommand(str, Connection);
                log.Info("DBCreationScripts: Lancio il comando di creazione database");
                myCommand.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                log.Info("DBCreationScripts: Eccezione nel comando di creazione database -  Message: " + ex.Message);
                throw ex;
            }
        }

问题是打开连接失败,实例是SQLEXPRESS,但是不明白为什么同事没有。 E ' 我可能配置有误吗?

SQL 服务都处于活动状态。

提示?

引用 SQL 服务器实例的正确方法是 Server Address\Instance Name,例如 MyServer\SQLExpress.\SQLEXPRESS127.0.0.1\SQLEXPRESS

如果没有反斜杠,文本将被解释为机器名称。带有 Server=SQLEXPRESS 的连接字符串将尝试连接到名为 SQLEXPRESS 的服务器。