public class BaseClass
{
public static SqlConnection DBCon() //下面连接数据库写的代码
{
return new SqlConnection("server=.;database=db_ExamOnline;uid=sa;pwd=");
}
public static void BindDG(GridView dg, string id, string strSql, string Tname)
{
SqlConnection conn = DBCon(); //创建连接数据库
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);//传入参数
DataSet ds = new DataSet(); //声明并且初始化dataSet
sda.Fill(ds, Tname); //填充
dg.DataSource = ds.Tables[Tname];//邦定数据源
dg.DataKeyNames = new string[] { id };//数组法?现在不提倡了 用起来麻烦
dg.DataBind(); //邦定到GridView 控件
}
public static void OperateData(string strsql) //将strsql传入此方法内
{
SqlConnection conn = DBCon(); //查ungjian连接数据库
conn.Open(); //打开数据库
SqlCommand cmd = new SqlCommand(strsql, conn); //执行参数传递过来的命令
cmd.ExecuteNonQuery(); //返回值
conn.Close(); //关闭数据库
}
//================如果是学生登录=========================================
public static bool CheckStudent(string studentNum, string studentPwd)
{
SqlConnection conn = DBCon(); //创建连接数据库
conn.Open(); //打开数据库
SqlCommand cmd = new SqlCommand("select count(*) from tb_Student where StudentNum='" + studentNum + "' and StudentPwd='" + studentPwd + "'", conn); //创建command命令
int i = Convert.ToInt32(cmd.ExecuteScalar()); //检测返回值
if (i > 0) //如果返回值i大于1则执行
{
return true;//返回正确
}
else //如果返回值i不大于1则执行这个
{
return false; //返回错误
}
conn.Close();//关闭数据库
}
//================如果是教师登录=========================================
public static bool CheckTeacher(string teacherNum, string teacherPwd)
{
SqlConnection conn = DBCon(); //创建连接数据库
conn.Open(); //打开数据库
SqlCommand cmd = new SqlCommand("select count(*) from tb_Teacher where TeacherNum='" + teacherNum + "' and TeacherPwd='" + teacherPwd + "'", conn); //创建command命令
int i = Convert.ToInt32(cmd.ExecuteScalar()); //检测返回值
if (i > 0) //如果返回值i大于1则执行
{
return true; //返回正确
}
else //如果返回值i不大于1则执行这个
{
return false; //返回错误
}
conn.Close(); //关闭数据库
}
//================如果是管理员登录=========================================
public static bool CheckAdmin(string adminNum, string adminPwd)
{
SqlConnection conn = DBCon(); //创建连接数据库
conn.Open(); //打开数据库
SqlCommand cmd = new SqlCommand("select count(*) from tb_Admin where AdminNum='" + adminNum + "' and adminPwd='" + adminPwd + "'", conn); //创建command命令
int i = Convert.ToInt32(cmd.ExecuteScalar()); //检测返回值
if (i > 0) //如果返回值i大于1则执行
{
return true; //返回正确
}
else //如果返回值i不大于1则执行这个
{
return false; //返回错误
}
conn.Close(); //关闭数据库
}
}
具体不明白的短信我就 ok了~
public class BaseClass
{
public static SqlConnection DBCon()
{
return new SqlConnection("server=.;database=db_ExamOnline;uid=sa;pwd="); 声明2000数据库
}
public static void BindDG(GridView dg, string id, string strSql, string Tname)
{ //通过static可以用类BaseClass直接调用BindDG这个方法
SqlConnection conn = DBCon(); 数据库连接
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn); //调用SqlDataAdapter这个类,为的是把数据从数据库弄出
DataSet ds = new DataSet(); //声明DataSet
sda.Fill(ds, Tname); //把数据添加到ds中
dg.DataSource = ds.Tables[Tname]; //把数据全都添加到dg中
dg.DataKeyNames = new string[] { id };
dg.DataBind(); //数据的绑定
}
public static void OperateData(string strsql)
{
SqlConnection conn = DBCon();
conn.Open();
SqlCommand cmd = new SqlCommand(strsql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
//================如果是学生登录=========================================
public static bool CheckStudent(string studentNum, string studentPwd)
{
SqlConnection conn = DBCon();
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from tb_Student where StudentNum='" + studentNum + "' and StudentPwd='" + studentPwd + "'", conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
//================如果是教师登录=========================================
public static bool CheckTeacher(string teacherNum, string teacherPwd)
{
SqlConnection conn = DBCon();
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from tb_Teacher where TeacherNum='" + teacherNum + "' and TeacherPwd='" + teacherPwd + "'", conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
//================如果是管理员登录=========================================
public static bool CheckAdmin(string adminNum, string adminPwd)
{
SqlConnection conn = DBCon();
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from tb_Admin where AdminNum='" + adminNum + "' and adminPwd='" + adminPwd + "'", conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
return true;
}
else
{
return false;
}
conn.Close();
}
}
以下的,只要你多敲,就自然明白其中的意思...
void
Login_Click()
{
if
(txtCode.Text.Trim()
!=
Session["verify"].ToString())
//判断填写的验证码是否和生成的一样
{
Response.Write("");
//跳转页面
}
else
{
if
(this.dropdownList.SelectedValue
==
"学生")
//如果下拉框选的是学生
{
if
(BaseClass.CheckStudent(txtNum.Text.Trim(),
txtPwd.Text.Trim()))
//判断学生的号数和密码
{
Session["ID"]
=
txtNum.Text.Trim();
//并把学生ID号赋给txtnum
Response.Redirect("student/studentexam.aspx");
//跳转页面
}
else
{
Response.Write("");
//跳转页面
}
}
if
(this.dropdownList.SelectedValue
==
"教师")
//如果下拉框选的是老师
{
if
(BaseClass.CheckTeacher(txtNum.Text.Trim(),
txtPwd.Text.Trim()))
//判断老师的号数和密码
{
Session["teacher"]
=
txtNum.Text;
//并把老师的ID号赋给txtnum
Response.Redirect("teacher/TeacherManage.aspx");
//跳转至管理页面
}
else
{
Response.Write("");
//如密码不正确
跳转到登录页面
}
}
整整一分钟,,,好累.
+分哈
static SqlConnection DBCon() 返回一个SQL的数据库连接
conn.Open(); 打开连接
SqlCommand cmd = new SqlCommand() ;添加要执行的SQL语句
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn); 执行SQL,返回指定内容
DataSet ds = new DataSet(); 创建数据集
sda.Fill(ds, Tname); 把返回的内容填充到数据集中,以Tname的内容为表名
dg.DataSource = ds.Tables[Tname]; 将数据集中以Tname的内容为表名的内容传给控件的数据源
dg.DataKeyNames = new string[] { id }; 设置关键字的名称
dg.DataBind(); 绑定数据,显示
cmd.ExecuteScalar() 执行添加的SQL语句
Convert.ToInt32 把字串转换成数字
conn.Close(); 关闭连接
- -!都是基础的问题 建议好好看下书