界面代码实现:
namespace Test01.View
{
public partial class StaffInfo : System.Web.UI.Page
{
Model.Functions con;
//界面加载时
protected void Page_Load(object sender, EventArgs e)
{
con = new Model.Functions();
ShowMessage();
}
//将数据库中的数据显示在GridView控件上(id=staffInfoGV)
private void ShowMessage()
{
string sqlQuery = "select UId as 编号,UName as 姓名,UDepartment as 部门,UAddress as 邮箱,UPhone as 电话 from UserTable";
staffInfoGV.DataSource = con.GetAllData(sqlQuery);
staffInfoGV.DataBind();
}
//条件查询
private void QuerybyNameOrDepartment(string name, string department)
{
string sqlQuery = "select UId as 编号,UName as 姓名,UDepartment as 部门,UAddress as 邮箱,UPhone as 电话 from UserTable where ";
//根据查询条件进行查询
if (name != "" && department != "")
{
sqlQuery += "UName like '%{0}%' and UDepartment like '%{1}%'";
sqlQuery = string.Format(sqlQuery, name, department);
}
else if (name != "")
{
sqlQuery += "UName like '%{0}%'";
sqlQuery = string.Format(sqlQuery, name);
}
else {
sqlQuery += "UDepartment like '%{0}%'";
sqlQuery=string.Format(sqlQuery, department);
}
staffInfoGV.DataSource = con.GetAllData(sqlQuery);
staffInfoGV.DataBind();
}
//点击查询
protected void querybtn_Click(object sender, EventArgs e)
{
if(nameQuery.Value=="" && departmentQuery.Value == "")//全部查询
{
ShowMessage();
}
else
{
QuerybyNameOrDepartment(nameQuery.Value,departmentQuery.Value);
}
}
//信息插入
protected void insertBtn_Click(object sender, EventArgs e)
{
try
{
string name = nametxt.Value;
string department = departmenttxt.Value;
string email = emailtxt.Value;
string phone=phonetxt.Value;
string sqlInsert = "insert into UserTable values('{0}','{1}','{2}','{3}')";
sqlInsert =string.Format(sqlInsert,name,department,email,phone);
con.SetData(sqlInsert);
ShowMessage();
ErrMsg.InnerText = "添加成功";
}
catch (Exception ex)
{
ErrMsg.InnerText = ex.ToString();
}
}
//选中GridView中的某条信息,将其显示在页面左侧的文本框中
protected void staffInfoGV_SelectedIndexChanged(object sender, EventArgs e)
{
//key = Convert.ToInt32(staffInfoGV.SelectedRow.Cells[1].Text);
nametxt.Value=staffInfoGV.SelectedRow.Cells[2].Text;
departmenttxt.Value=staffInfoGV.SelectedRow.Cells[3].Text;
emailtxt.Value=staffInfoGV.SelectedRow.Cells[4].Text;
phonetxt.Value=staffInfoGV.SelectedRow.Cells[5].Text;
}
//修改
protected void alterBtn_Click(object sender, EventArgs e)
{
string sqlupdate = "update UserTable set UName='{0}',Udepartment='{1}',UAddress='{2}',UPhone='{3}' where UId='{4}'";
sqlupdate = string.Format(sqlupdate, nametxt.Value, departmenttxt.Value, emailtxt.Value, phonetxt.Value, staffInfoGV.SelectedRow.Cells[1].Text);
con.SetData(sqlupdate);
ShowMessage();
ErrMsg.InnerText = "修改成功";
}
//删除
protected void deleteBtn_Click(object sender, EventArgs e)
{
string sqlDel = "delete from UserTable where UName='{0}' and UDepartment='{1}' and UAddress='{2}' and UPhone='{3}'";
sqlDel = string.Format(sqlDel, nametxt.Value, departmenttxt.Value, emailtxt.Value, phonetxt.Value);
con.SetData(sqlDel);
ShowMessage();
ErrMsg.InnerText = "删除成功";
}
//重置
protected void resetBtn_Click(object sender, EventArgs e)
{
nametxt.Value = "";
departmenttxt.Value = "";
emailtxt.Value = "";
phonetxt.Value = "";
}
}
}
登录模块功能实现代码如下:
namespace Test01.View
{
public partial class WebForm1 : System.Web.UI.Page
{
Model.Functions con;
protected void Page_Load(object sender, EventArgs e)
{
con=new Model.Functions();
}
protected void loginBtn_Click(object sender, EventArgs e)
{
if(nameInput.Value=="" || passwordInput.Value == "")
{
ErrMsg.InnerText = "用户名和密码不能为空!";
}
else
{
string sqlLogin = "select * from LoginTable where Name='{0}' and Password='{1}'";
sqlLogin=String.Format(sqlLogin, nameInput.Value,passwordInput.Value);
if (con.Login(sqlLogin))
{
Response.Redirect("StaffInfo.aspx");
}
else
{
ErrMsg.InnerText = "用户名或密码错误!";
}
}
}
protected void resetBtn_Click(object sender, EventArgs e)
{
nameInput.Value = "";
passwordInput.Value = "";
}
}
}
与数据库交互部分:
using System.Data.SqlClient;
using System.Data;
namespace Test01.Model
{
public class Functions
{
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataAdapter _adapter;
private DataTable _data;
private string constr;
public Functions() //构造函数
{
constr = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\Documents\\Data.mdf;Integrated Security=True;Connect Timeout=30"; //数据库连接字符串
_connection = new SqlConnection(constr); //新建数据库连接
_command = new SqlCommand();
_command.Connection = _connection;
}
public DataTable GetAllData(string sqlQuery) //查询数据库中的数据
{
_data = new DataTable();
_adapter=new SqlDataAdapter(sqlQuery,constr);
_adapter.Fill(_data);
return _data;
}
public int SetData(string sqlInsert)//数据库更新,包括增、删、改
{
int i = 0;
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
_command.CommandText = sqlInsert;
i=_command.ExecuteNonQuery();
_connection.Close();
return i;
}
public bool Login(string sqlLogin) //验证数据是否存在select * from table where name='{0}' and pwd='{1}';
{
bool flag = false;
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
_command.CommandText = sqlLogin;
SqlDataReader reader = _command.ExecuteReader(); //快速查询返回sqlDataReader实例
if (reader.HasRows) //判断返回数据是否为空
{
flag = true;
}
_connection.Close();
return flag;
}
}
}