简体   繁体   中英

Can't connect to database server

I'm encountering a problem with my database connection. I started with a new blank solution, then I added a WCF library project, and last but not least a WCF website (service). In the website i added a reference to the library where I have the interface (data contract), the class that implements the interface and the dataclasses. what I'm trying to do is to connect to a database on a server and try to retrieve some data from there. So the connection string looks like:

    <add name="myConnectionString" connectionString="Data Source=MyServer; Initial Catalog=MyDatabase; User Id=me; Password=me123;" providerName="System.Data.SqlClient" />

and this is how I'm trying to connect with the database:

    public List<string> GetEngagements(string id)
    {
        string sql = "SELECT myColumn FROM myTable WHERE Id = '" + id + "'";
        string connString = string.Empty;
        SqlConnection connDB;
        connString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        connDB = new SqlConnection(connString);
        SqlCommand command = new SqlCommand(sql, connDB);
        connDB.Open();
        SqlDataReader rdr = command.ExecuteReader();
        List<string> numbers = new List<string>();

        while (rdr.Read())
        {
            numbers.Add(rdr[0].ToString());
        }
        rdr.Close();

        return numbers;
    }

I'm getting an exception on connDB.Open(). Then exception message says: Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.

I've been getting this message for 2 days now, I've googled a lot and deleted the C:\\Documents and Settings\\username\\Local Settings\\Application Data\\Microsoft\\Microsoft SQL Server Data\\SQLEXPRESS directory but it didn't work for me..

Any solution???? help please

The error message:

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance.

Suggests that you're using user instancing , and therefore your connection string will point to an .mdf file on disk rather than the name of a database.

So I'll assume that you want to connect to a file instance rather than a server instance.

I'll also assume that you're using SqlExpress rather than the full fat version.

In which case your connection string is wrong. It should look more like this:

"Data Source=.\SQLEXPRESS;
 AttachDbFilename=fileOnDisk.mdf;
 Integrated Security=True;
 User Instance=True;"

User instancing means that this server instance and the DB inside will only be visible to the application opening the connection string.

You don't have to use user instancing - you can set User Instance=False or just leave it out. Then once the application has made the connection you can connect other tools to the server instance and connect to the DB yourself.

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