C#高手们指点指点小弟吧,我快晕了,帮我看看程序,提前谢谢了

来源:百度知道 编辑:UC知道 时间:2024/06/04 06:23:55
private void button1_Click(object sender, EventArgs e)
{

if (con.State == ConnectionState.Closed)
con.Open();
string str1 = "select USerName from tb_User ";
string str2 = "update tb_User set USerRight='" + textBox2.Text + "' where USerName='" + textBox1.Text + "'";
string str3 = textBox1.Text;
com = new SqlCommand();
com.Connection = con;
com.CommandText = str1;
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();

SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
while (dr.Read())
{
string cname = dr.GetString(0);
dr.Close();
if (str3 == cname)
{

真不知道现在的程序员哪学来的坏习惯.
if后面不加{}.少打2个符号能剩很多力气么????一旦错了.你找错误的时间比打字的时间多百倍不只.
你要实现的功能无非是要查找一个用户.有就修改用户权限.没有就提示没有用户撒.哪要写这么复杂.还用while循环.直接用SQL查询不就完了么?

SqlConnection con = new SqlConnection("连接字符串");
SqlCommand com;
SqlDataAdapter da;

private void button2_Click(object sender, EventArgs e)
{
string sql = "select USerName from tb_User where USerName = '" + textBox1.Text + "'";
da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
sql = "update tb_User set USerRight='" + textBox2.Text + "' where USerName='" + textBox1.Text + "'";
com = new SqlCommand(sql,con);
com.CommandType = CommandType.Text;