用户“sa"登录失败.在连接字符串中 [英] Login failed for user 'sa'. in connection string

查看:22
本文介绍了用户“sa"登录失败.在连接字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

用户sa"登录失败

当我尝试通过字符串变量设置值来连接服务器时:

When I try to connect server by setting value through a string variable:

private SqlConnection getDbConnection = new SqlConnection("Data Source="+dbname+";Initial Catalog="+catname+";User Id=sa;Password=sa;Integrated Security=false");

但是当我使用没有字符串变量的普通连接字符串时,它运行良好:

But when I use the normal connection string with no string variable it works well:

private SqlConnection getDbConnection = new SqlConnection("Data Source=Ali-pc\\;Initial Catalog=master;User Id=sa;Password=sa;Integrated Security=false");

推荐答案

将您的连接字符串放入 web.config 文件中,如下所示

put your connectionstring in web.config file as I show below

<connectionStrings>
<add name="Test" connectionString="data source=Harsh; Initial Catalog=Test ; user Id=sa; password=sa123;"/>
</connectionStrings>

Test"上面是connectionstringname,你可以写你的数据源名称.我的很苛刻,所以根据你的改变它.在初始目录中给出你的数据库名称.

Above "Test" is connectionstringname,you can write your datasource name.Mine is Harsh,so changed it according to yours.Give your database name in inital catalog.

然后将此代码放在页面加载事件后面的代码中

And then put this code in your code behind page load event

SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ToString());

它会起作用.

这篇关于用户“sa"登录失败.在连接字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆