帮我看看这段代码撒,那你有问题
提示
“WindowsApplication78.dbhelper”的类型初始值设定项引发异常。
class dbhelper
{
private static string connstring = "Data Sourse=.;Initial Catalog=master;Integrated Security=true";
public static SqlConnection connection = new SqlConnection(connstring);
}
public bool ceshi(string Userid, string Userpwd, string Userlevel)
{
int count = 0;
bool ceshi = false;
if (Userlevel == "管理员")
{
string sql = string.Format("select count(*) from master UserInfo where Userid='{0}' and Userpwd='{1}'", Userid, Userpwd);
try
{
SqlCommand cmd = new SqlCommand(sql, dbhelper.connection);
dbhelper.connection.Open();
count = (int)cmd.ExecuteScalar();
if (count == 1)
{
ceshi = true;
}
else
{
MessageBox.Show("用户名或密码不存在");
ceshi = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
dbhelper.connection.Close();
}
}
return ceshi;
}
private void button1_Click(object sender, EventArgs e)
{
if(ceshi(username.Text,password.Text,userleixing.Text))
{
MessageBox.Show("登陆成功");
}
}
还有这段
为什么提示UserInfo无效,UserInfo明明有的啊
public bool ceshi()
{
int count = 0;
bool ceshi = false;
string UserName = username.Text;
string number = password.Text;
string leixing = userleixing.Text;
if (leixing == "管理员")
{
string aa = "Data Source=.;Initial Catalog=master;Integrated Security=true";
SqlConnection con = new SqlConnection(aa);
con.Open();
string sql = string.Format("Select count(*) from UserInfo where Userid='{0}'and Userpwd='{1}'", UserName, number);
SqlCommand cmd = new SqlCommand(sql, con);
count = (int)cmd.ExecuteScalar();
try
{
if (count == 1)
{
ceshi = true;
}
else
{
MessageBox.Show("账户或密码错误");
ceshi = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
return ceshi;
}
private void button1_Click(object sender, EventArgs e)
{
if (ceshi())
{
MessageBox.Show("成功");
}
}