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;
}
//关键性代码 这里修改一下就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 + "'"; }
我确实不怎么懂编程,只是兴趣而已,这段代码我在本地vs2013中试了,没感觉存在问题,想实现的都能正常执行,但是放在IIS上的webservice中,返回了这段报错,很不理解,想看看园子是否能有人给出正确答案,在我心中这并不是最佳答案,但是在回复中,你是最想解答这个问题的人
一个 SqlConnection 对象 只能做一个 操作。
MultipleActiveResultSets=true
这个你可以用用
不知你要干嘛
所以说思维像星空,看的见,却看不懂
Data Source=***********;Initial Catalog=
和uid,pwd不要同时使用。
用database=;server=;替换。
@"server=******;uid=sa;pwd=*******;database=" + kuming + ";";
这只是换汤不换药吧,结局是一样的
这sql代码,绝对绝对是小白写的,
没有一颗助人的心,就别在这里鄙视他人了
@守墓老人: 哦,你误会了,
我给你理一下业务逻辑:要更新密码,说明用户已经登陆到系统中,不用再次验证该用户是否存在,直接更新用户密码就行
再更新时,要根据用户的旧密码和用户名,在一个事务中修改,这样,才能确保修改成功
update 用户 set password=new_password where username='xx' and password='yyy'
@悦光阴:正常情况确实是这样,但是这个是需要未登录情况修改改密码,谢了