简体   繁体   English

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

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

I am creating a client in WPF with C # , and I need to install it on the client ( tablet surface ) . 我正在使用C#在WPF中创建一个客户端,并且需要将其安装在客户端(平板电脑表面)上。 That said , I installed SQL Server 2014 Express edition , just to get the instance , I've downloaded from here : 就是说,我安装了SQL Server 2014 Express版,只是为了获取实例,我已经从这里下载了:

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

and i get this: 我得到这个:

Express 64BIT\\SQLEXPR_x64_ENU.exe Express 64BIT \\ SQLEXPR_x64_ENU.exe

I installed everything, of course I do not have the managament of SQL Server interface , but I can not create the db , here is my error: 我安装了所有内容,当然我没有SQL Server接口的管理,但是我无法创建db,这是我的错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server (提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接

this is my connection string (get this from constants file): 这是我的连接字符串(从常量文件中获取):

Server=SQLEXPRESS;Database=master;Integrated security=true; 服务器= SQLEXPRESS;数据库=主服务器;集成安全性=真;

and this is my code: 这是我的代码:

 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;
            }
        }

The problem is that it fails to open the connection , the instance is SQLEXPRESS , but do not understand why colleagues are not . 问题是它无法打开连接,实例是SQLEXPRESS,但不理解为什么同事不这样做。 E ' can I have configured something wrong ? 我可以为我配置一些错误吗?

The SQL services are all active . SQL服务都处于活动状态。

Tips ? 提示 ?

The correct way to refer to a SQL Server instance is Server Address\\Instance Name , eg MyServer\\SQLExpress , .\\SQLEXPRESS or 127.0.0.1\\SQLEXPRESS . 引用SQL Server实例的正确方法是Server Address\\Instance Name ,例如MyServer\\SQLExpress.\\SQLEXPRESS127.0.0.1\\SQLEXPRESS

Without the backslash, the text is interpreted as the machine name. 如果没有反斜杠,则将文本解释为计算机名称。 A connection string with Server=SQLEXPRESS will try to connect to a server named SQLEXPRESS . 带有Server=SQLEXPRESS连接字符串将尝试连接到名为SQLEXPRESS的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM