简体   繁体   中英

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 ) . That said , I installed SQL Server 2014 Express edition , just to get the instance , I've downloaded from here :

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

and i get this:

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:

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

this is my connection string (get this from constants file):

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

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 . E ' can I have configured something wrong ?

The SQL services are all active .

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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