con.open()连接错误 [英] con.open() Connection error

查看:90
本文介绍了con.open()连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是visual studio 2010 ASP.NET的新用户,我正在努力连接到我的数据库。我正在测试以使其运行的代码如下:

I am a new user to visual studio 2010 ASP.NET and I am struggling to connect to my database. The code I am testing to try and get it working is as follows:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
        con.Open();
        string Usercommand = "select count(*) from Access_Control where User_Name ='" + TextBox1.Text + "'";
        SqlCommand Checkuser = new SqlCommand(Usercommand, con);
        int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());

        if (temp == 1)
        {
            string Passcommand = "select Access_Control.User_password from Access_Control where User_Name ='" + TextBox1.Text + "'";
            SqlCommand Checkpass = new SqlCommand(Passcommand, con);
            string password = Checkpass.ExecuteScalar().ToString();

            if (password == TextBox2.Text)....





每次我开始没有调试,我的webapp的登录页面出现但是当我登录时,我总是收到以下错误消息:





Everytime I start without debugging, the login page for my webapp appears but as I log in I always get this error message :

Login failed for user 'MiThTiC-HP\AamirSQL'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'MiThTiC-HP\AamirSQL'.

Source Error: 


Line 24:     {
Line 25:         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
Line 26:         con.Open();
Line 27:         string Usercommand = "select count(*) from Access_Control where User_Name ='" + TextBox1.Text + "'";
Line 28:         SqlCommand Checkuser = new SqlCommand(Usercommand, con);







I我是新手,所以我不确定出了什么问题......如果有人可以请帮助我那将是非常棒的!



另外我想我应该注意:我上面发布的代码在我的大学服务器上运行...我试图在家里复制那个环境而且我一直得到这些错误




I am new to this so I am not sure what has gone wrong ... if someone could please help me that would be awesome !

Also I think I should NOTE: That the code which I have posted above works on my university server... I am trying to replicate that environment at home and I keep getting these errors

推荐答案

你好Aamir ,



我相信连接字符串是错误的。

尝试对连接字符串进行硬编码而不是

Hi Aamir,

Connection string is wrong in here I believe.
Try hard coding the connection string instead of
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);



喜欢这个


like this

SqlConnection con = new SqlConnection("data source=SQLExpress;User Id=sa;Password=123;Initial Catalog=DatabaseName;MultipleActiveResultSets=True");



如果是我远程数据库然后尝试从SQL Server IDE连接数据库。



我希望这会对你有所帮助。

谢谢,

RK


If it is remote database then try connecting the database from your SQL Server IDE.

I hope this would help you a bit.
Thanks,
RK


我的解决方案感谢venkateshCST:



他告诉我更改我的appsetting连接字符串在web.config中:

My solution what thanks to venkateshCST:

He told me to change my appsetting connectionstring within the web.config to be:
<add key="Award_Management2ConnectionString1" value="SERVER=(serverName);DATABASE=(DB name);UID=(userID);PASSWORD=(password)"/>





他还告诉我改变;

In我所在的地方 -





He also told me to change;
In places where I had -

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString);
        SqlConnection con = ConfigurationSettings.AppSettings["Award_Management2ConnectionString1"].ToString();
        sqlconnection con=new sqlconnection(constring);
        con.Open();





To -





To -

string constring = ConfigurationManager.AppSettings["Award_Management2ConnectionString1"].ToString();
        SqlConnection con = new SqlConnection(constring);
        con.Open();


尝试按如下方式指定连接字符串。





string connectionString = ConfigurationManager.ConnectionStrings [Award_Management2ConnectionString1]。ConnectionString;

SqlConnection con = new SqlConnection(connectionString);



希望这能解决您的问题。
Try to specify your connection string as follows.


string connectionString = ConfigurationManager.ConnectionStrings["Award_Management2ConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);

Hope this will resolve your issue.


这篇关于con.open()连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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