版主在么,做完登录界面后,程序崩溃,显示数据库登录失败: public static
DataSet ds = new DataSet();
public static string connstring = "server=.;database=ssxt;uid=sa;pwd=123456;";
//执行select命令,并返回结果写入数据集的表中
public bool
Select(string cmdstr, string tablename)
{
SqlConnection
conn = new SqlConnection(connstring);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds, tablename);
conn.Close();
return true;
}
public void Insert(string cmdstr)
{
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
public void Update(string cmdstr)
{
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
public void Delete(string cmdstr)
{
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
这个是我编写的类,运行后出现:
这个是我的按钮代码: sql login = new sql();
private void button1_Click(object sender, EventArgs e)
{
string strcmd;
if (txtloginname.Text != "" && txtpassword.Text != "")
{
strcmd = "select * from ygb where yg_id=" + txtloginname.Text + " and mm='" + txtpassword.Text + "'";
login.Select(strcmd, "login");
if (sql.ds.Tables["login"].Rows.Count != 0)
{
Form GLYJM = new frmlogin();
GLYJM.Show();
this.Hide();
}
else
MessageBox.Show("用户名或密码错误!");
}