简体   繁体   中英

Connecting to SQL Server 2008 R2 database from Visual Studio 2013

I know it is a classic problem. But I am fed up. I am trying to generate a Windows Forms application in VS 2013. For database I use SQL Server 2008 R2. The database and application are on the same system. And I use the following connection string in my app.config file

<connectionStrings>
<add  name="Connectionstring1" 
      connectionString="Data Source=PC02;Initial Catalog=KCPIMSTest;
      User ID=sa;Password=***********;Integrated Security=true"  
      providerName="System.Data.SqlClient"/>
 </connectionStrings>

I got this connection string by adding the database to server explorer of VS 2013 and take it from properties. But while running the application I get an exception on con.open(); :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Cannot open database "KCPIMSTest" requested by the login. The login failed.

Instead of Data Source=PC02 , I already tried localhost , sqlexpress and all.

These are the additional codes

  public void Save(string uname, string pwd)
  {
        string constring = getConnection();
        SqlConnection con = new SqlConnection(constring);

        if (con.State != ConnectionState.Open)
            con.Open();

        SqlCommand cmd = new SqlCommand("tblTest", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@UserName", uname);
        cmd.Parameters["@UserName"].Direction = ParameterDirection.Input;
        .....

        cmd.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("Data Inserted Succesfully");
    }

    public static string getConnection()
    {
        /*Reading Connection string from App.config File */
        string constr = ConfigurationManager.ConnectionStrings["Connectionstring1"].ConnectionString;
        return constr;
    }

If you're using a SQL server login, and assuming you have the correct password, remove Integrated Security=true from your connection string.

Reference: http://msdn.microsoft.com/en-US/library/ms254500(v=vs.80).aspx

PS Practice using using where possible (ie classes that implement IDisposable such as SqlConnection ).

You should provide:

  • either User ID and Password (when you use SQL Server login)
  • or set Integrated Security=true (when you use Windows login).

Don't use both at the same time.

Data Source=PC02;Initial Catalog=KCPIMSTest;
      User ID=sa;Password=***********

Remove Integrated Security . Then change your password=***** to password=youroriginalpasswordtext Then your login will work and

I think you copied this connection string from the ServerExplorer . The passwords will be Masked by default. So you should change the Mask to original Password itself.

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