public bool login(string username,string pwd)
{
bool flag=false;
string str =SqlHelper.constr;
string sql = "select count(1) from student where studentname='" + username + "' and loginpwd='" + pwd + "'";
int cont=Convert.ToInt32(SqlHelper.ExecuteScalar(sql));
if (cont>0)
{
flag=true; //这是登录的方法 借用了 SqlHeper 类
}
return flag;
}
MySchoolBLL bll = new MySchoolBLL();
private void button1_Click(object sender, EventArgs e)
{
// Delu de = new Delu();
string name1 = this.txtUser.Text;
string pwd = this.txtPwd.Text;
bool flag = bll.login(name1, pwd);
if (flag)
{
///// MessageBox.Shthisow("登录成功!!","提示");
FrmZhujiemian fz = new FrmZhujiemian();
fz.name =name1 ;
fz.Show();
this.Hide();
}
else ////////这是在窗体 显示层的 判断
{
MessageBox.Show("登录失败!!请输入正确的","提示");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySchool.Models;
namespace MySchool.DAL
{
public class XiuGaiStudentPwd
{
public bool XiugaiPwd(Student stu)
借用了 SqlHeper 类
{ bool flag = false; string sql = @"update Student set LoginPwd='" + stu.LoginPwd + "' where StudentName='" + stu.StudentName + "'"; int conut= SqlHelper.ExecuteNonQuery(sql); if (conut>0) { flag = true; } return flag; } }}
Student stu = new Student();
private void btnUpdatePwd_Click(object sender, EventArgs e)
{
bool flag=false;
stu.LoginPwd = txtReNewPwd.Text;
stu.StudentName = this.txtName.Text;
flag = bll.XiugaiPwd(stu);
if (flag==true)
{
MessageBox.Show("修改成功!!!","提示");
this.Close();
}
else
{
MessageBox.Show("修改失败","提示");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MySchool.Models
{
public class Student ///这是模型层 的实体类
{
public string StudentName { get; set; }
public string StudentNo { get; set; }
public string Sex { get; set; }
public string LoginPwd { get; set; }
public int GradeId { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string BornDate { get; set; }
public string Email { get; set; }
////////////////////////////////////////////
public string SubjectName { get; set; }
public int StudentResult { get; set; }
public DateTime DataTime { get; set; }
public string Subjectid { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; //sqlhelper 类
namespace MySchool.DAL
{
public static class SqlHelper
{
//用静态的方法调用的时候不用创建SQLHelper的实例
//Execetenonquery
// public static string Constr = "server=HAPPYPIG\\SQLMODEL;database=shooltest;uid=sa;pwd=6375196;";
public static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public static int id;
///
/// 执行NonQuery命令
///
///
///
///
public static int ExecuteNonQuery(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteNonQuery(cmdTxt, CommandType.Text, parames);
}
//可以使用存储过程的ExecuteNonquery
public static int ExecuteNonQuery(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
//判断脚本是否为空 ,直接返回0
if (string.IsNullOrEmpty(cmdTxt))
{
return 0;
}
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
if (parames != null)
{
cmd.CommandType = cmdtype;
cmd.Parameters.AddRange(parames);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static SqlDataReader ExecuteDataReader(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteDataReader(cmdTxt, CommandType.Text, parames);
}
//SQLDataReader存储过程方法
public static SqlDataReader ExecuteDataReader(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return null;
}
SqlConnection con = new SqlConnection(constr);
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType = cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
//把reader的行为加进来。当reader释放资源的时候,con也被一块关闭
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parames)
{
return ExecuteDataTable(sql, CommandType.Text, parames);
}
//调用存储过程的类,关于(ExecuteDataTable)
public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(sql))
{
return null;
}
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(sql, constr))
{
da.SelectCommand.CommandType = cmdType;
if (parames != null)
{
da.SelectCommand.Parameters.AddRange(parames);
}
da.Fill(dt);
return dt;
}
}
///
/// ExecuteScalar
///
/// 第一个参数,SQLServer语句
/// 第二个参数,传递0个或者多个参数
///
public static object ExecuteScalar(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteScalar(cmdTxt, CommandType.Text, parames);
}
//可使用存储过程的ExecuteScalar
public static object ExecuteScalar(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return null;
}
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType = cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
//调用存储过程的DBHelper类(关于ExeceutScalar,包含事务,只能处理Int类型,返回错误号)
public static object ExecuteScalar(string cmdTxt, CommandType cmdtype,SqlTransaction sqltran, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return 0;
}
using (SqlConnection con = new SqlConnection(constr))
{
int sum = 0;
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType=cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
sqltran = con.BeginTransaction();
try
{
cmd.Transaction = sqltran;
sum=Convert.ToInt32( cmd.ExecuteScalar());
sqltran.Commit();
}
catch (SqlException ex)
{
sqltran.Rollback();
}
return sum;
}
}
}
}
}