C# c中的等于是真的
我正在用c语言做webmethod。在调试方面,C# c中的等于是真的,c#,equals,webmethod,C#,Equals,Webmethod,我正在用c语言做webmethod。在调试方面, (chk.Equals(oldpass)) 查询在左侧和右侧显示相同的值 但是,执行并没有进入if内部,而是移到显示return语句的else部分。 福勒。这是我的密码 [WebMethod (Description="for change in password")] public string update(string authenid,string oldpass,string newpass) { SqlConnection
(chk.Equals(oldpass))
查询在左侧和右侧显示相同的值
但是,执行并没有进入if内部,而是移到显示return语句的else部分。
福勒。这是我的密码
[WebMethod (Description="for change in password")]
public string update(string authenid,string oldpass,string newpass)
{
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Workspace\\visual studio workspace\\Tmrepo\\App_Data\\tmrepo.mdf;Integrated Security=True;User Instance=True");
try
{
conn.Open();
string chk = "select pwd from client where authenid = '"+ @authenid +"' ";
if(chk.Equals(oldpass))
{
string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@authenid", authenid);
cmd.Parameters.AddWithValue("@oldpass", oldpass);
cmd.Parameters.AddWithValue("@newpass", newpass);
cmd.ExecuteNonQuery();
}
else
{
return "invalid oldpass";
}
conn.Close();
return newpass;
}
catch (Exception ex)
{
return ex.ToString();
}
}
这个代码有什么错误吗?我是c级新手。
谢谢。您的代码所做的是将authenid=some_值的select pwd from client与oldpass的值进行比较,后者将为false 固定代码逻辑:
string oldpass = "somehing";
string authenid = "pass_to_test";
string sql = string.Format("select pwd from client where authenid = '{0}' ", authenid);
string chk = null;
SqlCommand cmd = new SqlCommand(update, conn);
var reader = cmd.ExecuteReader();
if (reader.Read())
{
// has record with username
chk = reader.GetString(0);
if (chk.Equals(oldpass))
{
string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
cmd.CommandText = update;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@authenid", authenid);
cmd.Parameters.AddWithValue("@oldpass", oldpass);
cmd.Parameters.AddWithValue("@newpass", newpass);
cmd.ExecuteNonQuery();
}
else
{
return "invalid oldpass";
}
}
else
{
// not a valid username
}
reader.Close();
reader.Dispose();
您的代码所做的是将authenid=some_值与oldpass的值进行比较,后者将为false 固定代码逻辑:
string oldpass = "somehing";
string authenid = "pass_to_test";
string sql = string.Format("select pwd from client where authenid = '{0}' ", authenid);
string chk = null;
SqlCommand cmd = new SqlCommand(update, conn);
var reader = cmd.ExecuteReader();
if (reader.Read())
{
// has record with username
chk = reader.GetString(0);
if (chk.Equals(oldpass))
{
string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
cmd.CommandText = update;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@authenid", authenid);
cmd.Parameters.AddWithValue("@oldpass", oldpass);
cmd.Parameters.AddWithValue("@newpass", newpass);
cmd.ExecuteNonQuery();
}
else
{
return "invalid oldpass";
}
}
else
{
// not a valid username
}
reader.Close();
reader.Dispose();
您尚未执行命令:您的旧密码chk是:从客户端选择pwd,其中authenid='+@authenid+';,这是一个不太可能的密码。例如,查看ExecuteScalar 其他想法: 参数化sql-不要在中连接id;它应该是从客户端选择pwd,其中authenid=@authenid;其中,您正在添加一个名为authenid的参数,该参数的值来自authenid。在第二个ADO.NET查询中,您可以正确地看到这一点。 密码应该是咸的和散列的:不能直接存储;您永远不能提取和/或解密密码 外部编辑 更新:在authenid='+@authenid+'的客户机上执行代码作为selectpwd;因为从客户端选择pwd,其中authenid='@authenid';返回空值 更新2:
cmd.ExecuteScalar;成功了。并删除cmd.ExecuteNonQuery 您尚未执行命令:您的旧密码chk是:从客户端选择pwd,其中authenid='+@authenid+';,这是一个不太可能的密码。例如,查看ExecuteScalar 其他想法: 参数化sql-不要在中连接id;它应该是从客户端选择pwd,其中authenid=@authenid;其中,您正在添加一个名为authenid的参数,该参数的值来自authenid。在第二个ADO.NET查询中,您可以正确地看到这一点。 密码应该是咸的和散列的:不能直接存储;您永远不能提取和/或解密密码 外部编辑 更新:在authenid='+@authenid+'的客户机上执行代码作为selectpwd;因为从客户端选择pwd,其中authenid='@authenid';返回空值 更新2:
cmd.ExecuteScalar;成功了。并删除cmd.ExecuteNonQuery 终于解决了!!!我使用了数据集。代码完全正确的第二种方法是:
string chk = "select pwd from client where authenid = @authenid";
SqlCommand cmd1 = new SqlCommand(chk , conn);
cmd1.Parameters.AddWithValue("@authenid", authenid);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(ds);
int cnt = ds.Tables[0].Rows.Count;
if (cnt > 0)
{
if (ds.Tables[0].Rows[0]["pwd"].ToString().Equals(oldpass))
{
string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@authenid", authenid);
cmd.Parameters.AddWithValue("@oldpass", oldpass);
cmd.Parameters.AddWithValue("@newpass", newpass);
cmd.ExecuteNonQuery();
}
}
希望它能帮助像我这样的新手 终于解决了!!!我使用了数据集。代码完全正确的第二种方法是:
string chk = "select pwd from client where authenid = @authenid";
SqlCommand cmd1 = new SqlCommand(chk , conn);
cmd1.Parameters.AddWithValue("@authenid", authenid);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(ds);
int cnt = ds.Tables[0].Rows.Count;
if (cnt > 0)
{
if (ds.Tables[0].Rows[0]["pwd"].ToString().Equals(oldpass))
{
string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@authenid", authenid);
cmd.Parameters.AddWithValue("@oldpass", oldpass);
cmd.Parameters.AddWithValue("@newpass", newpass);
cmd.ExecuteNonQuery();
}
}
希望它能帮助像我这样的新手 首先提取密码的代码在哪里?Oldpassa也是查询吗?设置在哪里?发布了complt代码…此代码将密码值与字符串select pwd from client(其中authenid=1)进行比较。如果用户确实拥有该密码,它将转到else分支。完成您丢失的代码,我现在可以确定您是如何使用的!但您所做的是构造一个sql字符串,并根据密码检查它!首先提取密码的代码在哪里?Oldpassa也是查询吗?设置在哪里?发布了complt代码…此代码将密码值与字符串select pwd from client(其中authenid=1)进行比较。如果用户确实拥有该密码,它将转到else分支。完成您丢失的代码,我现在可以确定您是如何使用的!但您所做的是构造一个sql字符串,并根据密码检查它!不要使用String.Format生成查询。@Shachi-代码是一个逻辑,您需要根据需要进行调整@Shachi-更新了代码,缺少一行,cmd.CommandText=update;需要设置updatecommand的sql!不要使用String.Format生成查询。@Shachi-代码是一个逻辑,您需要根据需要进行调整@Shachi-更新了代码,缺少一行,cmd.CommandText=update;需要设置updatecommand的sql+1,这种类型的密码众所周知是有效的,需要特殊情况+1,这种类型的密码众所周知是有效的,需要特殊情况;