![](/img/trans.png)
[英]How to add my SQL Server 2008 R2 database to my Visual Studio 2010 Setup File?
[英]Connecting to SQL Server 2008 R2 database from Visual Studio 2013
我知道这是一个经典的问题。 但是我受够了。 我试图在VS 2013中生成Windows窗体应用程序。对于数据库,我使用SQL Server 2008 R2。 数据库和应用程序在同一系统上。 我在我的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>
我通过将数据库添加到VS 2013的服务器资源管理器并从属性中获取该连接字符串。 但是,在运行应用程序时,我在con.open();
上con.open();
了异常con.open();
:
System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常
附加信息:无法打开登录请求的数据库“ KCPIMSTest”。 登录失败。
代替Data Source=PC02
,我已经尝试了localhost
, sqlexpress
等。
这些是附加代码
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;
}
如果您使用的是SQL Server登录名,并假设您具有正确的密码,请从连接字符串中删除Integrated Security=true
。
参考: http : //msdn.microsoft.com/zh-CN/library/ms254500(v=vs.80).aspx
PS使用的实践using
在可能的情况(即,实现类IDisposable
如SqlConnection
)。
您应提供:
User ID
和Password
(使用SQL Server登录名时) Integrated Security=true
(使用Windows登录名时)。 请勿同时使用两者。
Data Source=PC02;Initial Catalog=KCPIMSTest;
User ID=sa;Password=***********
删除Integrated Security
。 然后将您的password=*****
更改为password=youroriginalpasswordtext
然后您的登录名将password=youroriginalpasswordtext
,
我认为您是从ServerExplorer
复制了此connection string
。 默认情况下,密码将被Masked
。 因此,您应该将掩码更改为original Password
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.