using System;
using System.Collections.Generic;
using System.Text;
using System.Data; //所有的一般数据访问类
using System.Data.Sql; //用于SQL Server数据访问的通用新接口和类
using System.Data.SqlClient; //SQL Server提供程序的类
using System.Data.SqlTypes; //SQL Server数据类型
using System.Data.Odbc;
using System.Configuration; //在项目中要添加对System.Configuration的引用
namespace StudentManagementSystem
{
class c_UIM //用户信息管理
{
//SqlConnection conn = new SqlConnection("server=.;pwd=sun;user id=sa;database=SIM"); //数据库连接
private static string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
//SqlConnection conn = c_conn.sqlconn(); //调用数据库连接类
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
public c_UIM()
{
}
public bool b_UIM(string str) //方法,按用户名查询
{
string str_userInfo = str.Trim(); //接收用户名同时,去掉左右空格
bool b_TF;
try
{
conn.Open(); //打开数据库
= "select * from tb_AddUserInfo where t_UserName='" + str_userInfo + "' collate Chinese_PRC_CS_AI_WS"; //按用户名精确查找
cmd.Connection = conn;
dr = cmd.ExecuteReader(); //执行SQL查询语句
if (dr.Read())
{
b_TF = true;
}
else
{
b_TF = false;
}
conn.Close(); //关闭数据库
}
finally
{
conn.Close(); //关闭数据库
}
return b_TF;
}
public bool b_NameUIM(string name,string CD,string sex) //方法,模糊查询(姓名、所在院系和性别)
{
string str_name = name.Trim(); //接收姓名同时去掉左右空格
string str_CD = CD; //接收所在院系
string str_sex = sex; //接收姓别
bool b_TF;
string str_select;
if (str_CD == "" && str_sex == "") //没有选择所在院系和性别
{
str_select = "select * from tb_AddUserInfo where t_name='" + str_name + "' collate Chinese_PRC_CS_AI_WS"; //按姓名查找
}
else if (str_CD == "")
{
str_select = "select * from tb_AddUserInfo where t_name='" + str_name + "'and t_sex='" + str_sex + "' collate Chinese_PRC_CS_AI_WS"; //按姓名、性别查找
}
else if (str_sex == "")
{
str_select = "select * from tb_AddUserInfo where t_name='" + str_name + "'and t_CollegeDepartmentID='" + str_CD + "' collate Chinese_PRC_CS_AI_WS"; //按姓名、所在院系查找
}
else
{
str_select = "select * from tb_AddUserInfo where t_name='" + str_name + "'and t_CollegeDepartmentID='" + str_CD + "'and t_sex='" + str_sex + "' collate Chinese_PRC_CS_AI_WS"; //按姓名、所在院系、性别查找
}
try
{
conn.Open(); //打开数据库
= str_select; //按要求查询
cmd.Connection = conn;
dr=cmd.ExecuteReader(); //执行SQL查询语句
if (dr.Read())
{
b_TF = true;
}
else
{
b_TF = false;
}
conn.Close();
}
finally
{
conn.Close(); //关闭数据库
}
return b_TF;
}
public bool b_del(string str) //删除用户信息表中(tb_AddUserInfo)数据
{
string str_userName = str;
bool b_TF;
try
{
conn.Open();
= "delete from tb_AddUserInfo where t_UserName='" + str_userName + "'collate Chinese_PRC_CS_AI_WS"; //按用户名,删除tb_AddUserInfo两表中的内容
cmd.Connection = conn;
cmd.ExecuteNonQuery();
b_TF = true;
}
finally
{
conn.Close();
}
return b_TF;
}
public bool B_AUIdel(string str) //删除添加新用户信息表中数据
{
string str_userName = str;
bool b_TF;
try
{
conn.Open();
= "delete from tb_UserInfo where t_UserName='" + str_userName + "'collate Chinese_PRC_CS_AI_WS"; //按用户名,删除tb_UserInfo两表中的内容
cmd.Connection = conn;
cmd.ExecuteNonQuery();
b_TF = true;
}
finally
{
conn.Close();
}
return b_TF;
}
}
}
private void but_Del_Click(object sender, EventArgs e)
{
c_UIM del_UIM = new c_UIM();
if(del_UIM.b_del(lab_name.Text))
{
if (del_UIM.B_AUIdel(lab_name.Text))
{
MessageBox.Show("数据已删除!");
}
}
}