Reputation: 5293
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;
}
Upvotes: 0
Views: 5597
Reputation: 4754
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
P.S. Practice using using
where possible (i.e. classes that implement IDisposable
such as SqlConnection
).
Upvotes: 3
Reputation: 899
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.
Upvotes: 1
Reputation: 43023
You should provide:
User ID
and Password
(when you use SQL Server login) Integrated Security=true
(when you use Windows login). Don't use both at the same time.
Upvotes: 1