c#登陆问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:32:13
string s = "DATABASE=Northwind;SERVER=localhost;Integrated Security=True;";
SqlConnection conn = new SqlConnection(s);
conn.Open();
string str="select * from login where userID='"+textBox1.Text+"'and password='"+textBox2.Text+"'";
SqlCommand cmd =new SqlCommand(str,conn);
cmd.EndExecuteNonQuery();
SqlDataAdapter da=new SqlDataAdapter(str,conn);
DataSet mydataset=new DataSet();
da.Fill(mydataset,"mytable");
SqlDataReader dr1 = cmd.ExecuteReader();
if (dr1.Read())
{
Form f = new Form2();
f.Show();
this.Hide();
}
else
{
MessageBox.Show(&

你的写法不妥当,Read()返回是一个对象,不是布尔型数据,C语言可以这样,但C#不行。
最好写成
"select count(*) from login where userID='"+textBox1.Text+"'and password='"+textBox2.Text+"'"
只需要SQLCOMMAND对象,里面的ExecuteScalar(),返回第一行的结果,不为0则NEW你的窗体。
下面的DATASET,DATAADAPTER全干掉。

Form f = new Form2();
这句有问题吧 为什么是form2 ?

试试这样 SqlServer 是不是把login作为保留关键字了?
select * from [login] where userID =。。。

string s = "DATABASE=Northwind;SERVER=localhost;Integrated Security=SSPI";
SqlConnection conn = new SqlConnection(s);
conn.Open();
string str="select * from login where userID='"+textBox1.Text+"'and password='"+textBox2.Text+"'";
SqlCommand cmd =new SqlCommand(str,conn);

SqlDataReader dr1 = cmd.ExecuteReader();
if (dr1.Read())
{
For