首页 新闻 会员 周边

数据库连接问题

-1
悬赏园豆:200 [已解决问题] 解决于 2017-03-20 10:31
String constr = @"Data Source=***********;Initial Catalog=" + kuming + ";User ID=sa;Password=***************;MultipleActiveResultSets=true";
SqlConnection con = new SqlConnection(constr);

con.Open();
string sql = "select * from DH_HH where name='" + username + "' and mima='" + password + "'";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
string SQL = @"update DH_HH set mima='" + passnew + "' where name='" + username + "'";
SqlCommand coms = new SqlCommand(SQL,con);
if (coms.ExecuteNonQuery() == 1)
xiugai = "true";
}
reader.Close();//关闭执行
con.Close();//关闭数据库连接//

                            false'System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader() at WebService1.Service1.yanzheng(String json) in c:\Users\Administrator\Desktop\android\C#webservice\WebServiceHYWT\WebService1\Service1.asmx.cs:line 168'


                                                            第一个if语句为168行
问题补充:

 public string yanzheng(string json)
        {
            string xiugai = "false";

            JsonData jd = JsonMapper.ToObject(json);
            int ss = jd.Count;
            string username = jd["username"].ToString();
            string password = jd["password"].ToString();
            string passnew = jd["passnew"].ToString();
            string kuming = jd["kuming"].ToString();
            try
            {                                 
                String constr = @"Data Source=******;Initial Catalog=" + kuming + ";User ID=sa;Password=******;MultipleActiveResultSets=true";
                SqlConnection con = new SqlConnection(constr);                                                                   
                con.Open();
                string sql = "select * from DH_HH where name='" + username + "' and mima='" + password + "'";
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader reader = com.ExecuteReader();
                if (reader.Read())
                {
                    string SQL = @"update DH_HH set mima='" + passnew + "' where name='" + username + "'";
                    SqlCommand coms = new SqlCommand(SQL,con);
                    if (coms.ExecuteNonQuery() == 1)
                    xiugai = "true";
                }
                reader.Close();//关闭执行
                con.Close();//关闭数据库连接
            }
            catch (Exception ex)
            {
                xiugai = "false'"+ex+"'";
            }

            return xiugai;
        }

 

 

守墓老人的主页 守墓老人 | 初学一级 | 园豆:26
提问于:2017-03-16 16:35
< >
分享
最佳答案
0

//关键性代码 这里修改一下就OK了

 

 

try
            {
                String constr = @"Data Source=.;Initial Catalog=FileShare;User ID=sa;Password=kfkz2015;";
                SqlConnection con = new SqlConnection(constr);
                con.Open();
                string sql = "select * from Table_2 where UName='" + username + "' and UPwd='" + password + "'";
                SqlCommand com = new SqlCommand(sql, con);
                using (SqlDataReader reader = com.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        reader.Close();
                        string SQL = @"update Table_2 set UPwd='" + passnew + "' where UName='" + username + "'";
                        SqlCommand coms = new SqlCommand(SQL, con);
                        if (coms.ExecuteNonQuery() == 1)
                            xiugai = "true";
                    }
                    con.Close();//关闭数据库连接
                }

            }
            catch (Exception ex)
            {
                xiugai = "false'" + ex + "'";
            }
收获园豆:200
需要格局 | 老鸟四级 |园豆:2145 | 2017-03-17 09:11

我确实不怎么懂编程,只是兴趣而已,这段代码我在本地vs2013中试了,没感觉存在问题,想实现的都能正常执行,但是放在IIS上的webservice中,返回了这段报错,很不理解,想看看园子是否能有人给出正确答案,在我心中这并不是最佳答案,但是在回复中,你是最想解答这个问题的人

守墓老人 | 园豆:26 (初学一级) | 2017-03-20 10:42
其他回答(4)
1

一个 SqlConnection 对象 只能做一个 操作。

pengbg | 园豆:13 (初学一级) | 2017-03-16 17:27

MultipleActiveResultSets=true这个你可以用用

支持(0) 反对(0) 守墓老人 | 园豆:26 (初学一级) | 2017-03-16 17:28
1

不知你要干嘛

龙行天涯 | 园豆:1794 (小虾三级) | 2017-03-16 17:51

所以说思维像星空,看的见,却看不懂

支持(0) 反对(1) 守墓老人 | 园豆:26 (初学一级) | 2017-03-16 18:24
1

Data Source=***********;Initial Catalog=

和uid,pwd不要同时使用。

用database=;server=;替换。

Supper_litt | 园豆:827 (小虾三级) | 2017-03-16 19:13

@"server=******;uid=sa;pwd=*******;database=" + kuming + ";";

这只是换汤不换药吧,结局是一样的

 

支持(0) 反对(0) 守墓老人 | 园豆:26 (初学一级) | 2017-03-16 19:40
0

这sql代码,绝对绝对是小白写的,

悦光阴 | 园豆:2251 (老鸟四级) | 2017-03-17 13:57

没有一颗助人的心,就别在这里鄙视他人了

支持(0) 反对(0) 守墓老人 | 园豆:26 (初学一级) | 2017-03-20 10:46

@守墓老人: 哦,你误会了,

我给你理一下业务逻辑:要更新密码,说明用户已经登陆到系统中,不用再次验证该用户是否存在,直接更新用户密码就行

再更新时,要根据用户的旧密码和用户名,在一个事务中修改,这样,才能确保修改成功

update 用户 set password=new_password where username='xx' and password='yyy'

支持(0) 反对(0) 悦光阴 | 园豆:2251 (老鸟四级) | 2017-03-20 12:21

@悦光阴:正常情况确实是这样,但是这个是需要未登录情况修改改密码,谢了

支持(0) 反对(0) 守墓老人 | 园豆:26 (初学一级) | 2017-03-20 15:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册