怎么通过C#程序实现判断输入文本框信息和数据库某一列值的匹配问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 10:14:30
比如一个程序的修改密码操作。。在修改密码之前要判断原始密码的正确性怎么解决。。下面是我自己弄的一个改密码的代码。怎修改呢 private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text.Trim() == string.Empty)
{MessageBox.Show("密码不能为空");
textBox3.Focus();
}
else if(textBox2.Text.Trim()!=textBox3.Text.Trim())
{
MessageBox.Show("两次原密码输入不一样");
textBox2.Focus();
}
else
{
//设置用户字符串
string connectionString = @"Data Source=xiaotu\sqlexpress;Initial Catalog=XKXT;Integrated Security=True;Pooling=False";
//建立连接。
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();

SqlCommand myCommand = myConnection.CreateCommand();

你得用dataRead方法把数据库里的数据读出来然后比较,你和sql语句比较怎么比啊,
string sql=string.farmat(@"select count(*) from t_User where password ={0}andadmin={1}",textBox2.Text.Trim(),textBox1.Text.Trim() );这样是把界面输入的放到SQL语句里面去了,
count(*)返回的是受影响的行数,如果有这个密码则正确,如果对不上密码就不正确,也就是说只要count>0则密码正确。
SqlCommand myCommand = myConnection.CreateCommand(sql,connection);
connection.open();
count=(int)command.ExecuteScalar();
if(count=0){密码不正确}
else{密码正确}

验证文本框是否为空以及前后密码是否一致的问题可以使用验证控件来实现不用写那么多代码的,下面是一个例子:
Class_data cd = new Class_data();//实例化数据库访问公共类
//public bool sql_cunzai(string str_command2)//检测登陆用户存在的方法
//{
// myconnection.Open();
// SqlCommand mycommand = new SqlCommand(str_command2, myconnection);
// mycommand.ExecuteNonQuery();
// SqlDataReader myreader = mycommand.ExecuteReader();
// myreader.Read();
// if (myreader.HasRows)
// {