代码如下,望高手指教,是不是我的textBox2.Text.ToString().Trim() == ds.Tables[0].ToString().Trim() 这个语句有错,望解答
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text != "") //当用户名不为空时开始执行下面代码
{
if(textBox2.Text !="")//当密码不为空时开始执行下面代码
{
string sqlcon = "Data Source=zhangchicompute;Initial Catalog=user;Integrated Security=True";
//然后实例化一个对象SqlConnection用于打开连接
SqlConnection conn = new SqlConnection(sqlcon);
conn.Open();//打开连接
string sql = "select 密码 from Table1 where 用户名 = '" + textBox1.Text + "'"; //sql语句
SqlCommand cmd = new SqlCommand(sql, conn); //实例化一个Command命令,调用SQL语句
SqlDataReader dr = cmd.ExecuteReader();//执行读取数据命令
//如果想获得数据,直接调用dr.GetString(下标); 下标为表中的第几列数据,例如获取第一列
dr.Read();//调用read方法读取SqlReader
if (dr.HasRows)//使用HasRows属性判断结果中是否有数据,即能够获取用户名时执行
{
//string md = "select 密码 from Table1 where 用户名 = '" + textBox1.Text + "'";
dr.Close();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "Table1");
if ( textBox2.Text.ToString().Trim() == ds.Tables[0].ToString().Trim() )
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("密码错误");
}
}
else
{
MessageBox.Show("用户名不存在");
}
conn.Close();//最后关闭连接
}
else
{
MessageBox.Show("请输入密码");
}
}
else
{
MessageBox.Show("请输入用户名");
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text != "") //当用户名不为空时开始执行下面代码
{
if(textBox2.Text !="")//当密码不为空时开始执行下面代码
{
string sqlcon = "Data Source=zhangchicompute;Initial Catalog=user;Integrated Security=True";
//然后实例化一个对象SqlConnection用于打开连接
SqlConnection conn = new SqlConnection(sqlcon);
conn.Open();//打开连接
string sql = "select 密码 from Table1 where 用户名 = '" + textBox1.Text + "'"; //sql语句
SqlCommand cmd = new SqlCommand(sql, conn); //实例化一个Command命令,调用SQL语句
SqlDataReader dr = cmd.ExecuteReader();//执行读取数据命令
//如果想获得数据,直接调用dr.GetString(下标); 下标为表中的第几列数据,例如获取第一列
dr.Read();//调用read方法读取SqlReader
if (dr.HasRows)//使用HasRows属性判断结果中是否有数据,即能够获取用户名时执行
{
//string md = "select 密码 from Table1 where 用户名 = '" + textBox1.Text + "'";
dr.Close();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "Table1");
if ( textBox2.Text.ToString().Trim() == ds.Tables[0].ToString().Trim() )
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("密码错误");
}
}
else
{
MessageBox.Show("用户名不存在");
}
conn.Close();//最后关闭连接
}
else
{
MessageBox.Show("请输入密码");
}
}
else
{
MessageBox.Show("请输入用户名");
}
}
你这里没有指定字段Tables[0].ToString() 出来的因该是这个对象的类型,如果没有重写Tables[0].ToString()方法的话。
应该为
if ( textBox2.Text.ToString().Trim() == ds.Tables[0].Rows[0][0].ToString().Trim() )
================你的代码================
if ( textBox2.Text.ToString().Trim() == ds.Tables[0].ToString().Trim() )
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("密码错误");
}
}
else
{
MessageBox.Show("用户名不存在");
}
conn.Close();//最后关闭连接
}
else
{
MessageBox.Show("请输入密码");
}
}
else
{
MessageBox.Show("请输入用户名");
}
========================================
中有一句:
if ( textBox2.Text.ToString().Trim() == ds.Tables[0].ToString().Trim() )
ds.Tables[0].ToString().Trim() 是什么? Tables[0] 有行,有列吧?读取行和列值,才能继续做判断呀。ds.Tables[0].Rows[0]["password"].ToString().Trim()
个人看法,仅供参考.