[英]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.
我试图在VS 2013中生成Windows窗体应用程序。对于数据库,我使用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 我在我的
app.config
文件中使用以下连接字符串
<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. 我通过将数据库添加到VS 2013的服务器资源管理器并从属性中获取该连接字符串。 But while running the application I get an exception on
con.open();
但是,在运行应用程序时,我在
con.open();
上con.open();
了异常con.open();
: :
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常
Additional information: Cannot open database "KCPIMSTest" requested by the login.
附加信息:无法打开登录请求的数据库“ KCPIMSTest”。 The login failed.
登录失败。
Instead of Data Source=PC02
, I already tried localhost
, sqlexpress
and all. 代替
Data Source=PC02
,我已经尝试了localhost
, sqlexpress
等。
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. 如果您使用的是SQL Server登录名,并假设您具有正确的密码,请从连接字符串中删除
Integrated Security=true
。
Reference: http://msdn.microsoft.com/en-US/library/ms254500(v=vs.80).aspx 参考: http : //msdn.microsoft.com/zh-CN/library/ms254500(v=vs.80).aspx
PS Practice using using
where possible (ie classes that implement IDisposable
such as SqlConnection
). PS使用的实践
using
在可能的情况(即,实现类IDisposable
如SqlConnection
)。
You should provide: 您应提供:
User ID
and Password
(when you use SQL Server login) User ID
和Password
(使用SQL Server登录名时) Integrated Security=true
(when you use Windows login). Integrated Security=true
(使用Windows登录名时)。 Don't use both at the same time. 请勿同时使用两者。
Data Source=PC02;Initial Catalog=KCPIMSTest;
User ID=sa;Password=***********
Remove Integrated Security
. 删除
Integrated Security
。 Then change your password=*****
to password=youroriginalpasswordtext
Then your login will work and 然后将您的
password=*****
更改为password=youroriginalpasswordtext
然后您的登录名将password=youroriginalpasswordtext
,
I think you copied this connection string
from the ServerExplorer
. 我认为您是从
ServerExplorer
复制了此connection string
。 The passwords will be Masked
by default. 默认情况下,密码将被
Masked
。 So you should change the Mask to original Password
itself. 因此,您应该将掩码更改为
original Password
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.