62,041
社区成员
发帖
与我相关
我的任务
分享
public class DataBase
{
public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//string strCon = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestOnline";
private SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestOnline");
private SqlCommand SqlCmd;
// private SqlDataAdapter da;
// private DataSet ds;
public int GetUser(string ComStr)
{
SqlCon.Open();
SqlCmd = new SqlCommand(ComStr, SqlCon);
int i;
i = Convert.ToInt32(SqlCmd.ExecuteScalar());
return i;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataBase db = new DataBase();
if (this.TextBox1.Text.Trim() == "" || this.TextBox2.Text.Trim() == "" || this.DropDownList1.SelectedValue == "")
{
Response.Write("<script>alert(\"账号,密码,用户类型不能为空\");</script>");
}
else
{
switch (DropDownList1.SelectedValue)
{
case "学生":
string SelCmd = "SELECT COUNT(*) FROM Student WHERE Name=" + this.TextBox1.Text + "and Pwd='" + this.TextBox2.Text + "'";
// int tmp = db.GetUser(SelCmd);
// Response.Write(tmp.GetType());
if (db.GetUser(SelCmd) != 0) //System.Data.SqlClient.SqlException: 将数据类型 varchar 转换为 numeric 时出错。
{
Session["StuNo"] = this.TextBox1.Text;
Response.Write("<script>alert(\"登陆成功\");</script>");
}
else
{
Response.Write("<script>alert(\"账号或密码错误\");<script>");
}
break;
default:
// Session["StuNo"] = this.TextBox1.Text;
// Response.Write(Session.ToString());
break;
}
}
}
//Name是什么类型?应该是文本型吧?所以加了两个"'"号,另外,"and"前面应该加一个空格
string SelCmd = "SELECT COUNT(*) FROM Student WHERE Name='" + this.TextBox1.Text + "' and Pwd='" + this.TextBox2.Text + "'";