客户管理系统中需要有database类和common类两个公共类、database了中的方法主要是用来与数据CustomerManagement打交道,common类中的方法主要是有关MD5加密方法和有关控件与数据库中的数据绑定的方法。
公共类database
database类包含打开数据库CustomerManagement的方法,关闭数据库的方法,创建SqlCommand命令方法,执行Sql语句方法以及将Datareader对象转换成DataTable对象的方法。
具体的代码为:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空间
using System.Data;
namespace 客户管理系统
{
class database//公共类database
{
SqlConnection con;//创建Connection对象
private void open()
{
if(con==null)
{
string Str="server=.;user=sa;pwd=123456;database=CustomerManagement";//数据库连接字符串
con=new SqlConnection(Str);//连接数据库
}
if(con.State==System.Data.ConnectionState.Closed)
{
con.Open();//打开
}
}
private void close()
{
if (con != null)
{
con.Close();//关闭
}
}
private SqlCommand createcommand(string sqlStr)
{
open();//打开数据库
SqlCommand comStr = new SqlCommand(sqlStr, con);//创建SqlCommand对象
return comStr;
}
public void runSql(string sqlStr)//执行sql语句
{
SqlCommand cmd = createcommand(sqlStr);//调用createcommand方法
cmd.ExecuteNonQuery();//执行sql语句
close();//关闭数据库
}
public int runSqlInt(string sqlStr)//执行sql语句
{
SqlCommand cmd = createcommand(sqlStr);
int t = (int)cmd.ExecuteScalar();//执行sql语句
close();
return t;
}
public SqlDataReader runSql(string sqlStr, out SqlDataReader dr)//执行sql查询语句
{
SqlCommand cmd = createcommand(sqlStr);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
public DataTable ConvertDatareaderTable(SqlDataReader dataReader)//将DateReader对象转换成DataTable对象
{
DataTable dt = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
try
{
foreach (DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = myRow[0].ToString();
dt.Columns.Add(myDataColumn);//添加列
}
while (dataReader.Read())
{
DataRow myDataRow = dt.NewRow();//创建列
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
myDataRow[i] = dataReader[i].ToString();
}
dt.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return dt;
}
catch (Exception ex)
{
throw new Exception("转换出错!", ex);
}
}
}
}</span>
公共类common
common类主要是创建一些与控件绑定的方法和一些加密方法,此类中包含32位MD5加密方法,将sql语句绑定到ComboBox控件方法,将sql语句绑定到ListBox控件方法,将sql语句生成DataTable对象的方法,将sql语句绑定到DataGridView控件的方法及根据值指定ComboBox控件索引的方法,其中MD5加密方法主要是处理管理员的密码。
具体的common类的代码为:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;//引入命名空间
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace 客户管理系统
{
class common
{
database db = new database();
public string md5(string pwd)//生成MD5加密后的字符串
{
string returnPwd = "";
MD5 md5 = MD5.Create();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(pwd);
bytes = md5.ComputeHash(bytes);
md5.Clear();
for (int i = 0; i < bytes.Length; i++)
{
returnPwd += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return returnPwd.PadLeft(32, '0');
}
public void bindComboBox(ComboBox cb, string sql)//绑定ComboBox控件
{
DataTable dt = new DataTable();
dt = createSource(sql);//将sql语句返回DataTable
cb.DataSource = dt;//设置数据源
cb.DisplayMember = dt.Columns[1].ColumnName;//绑定Text属性
cb.ValueMember = dt.Columns[0].ColumnName;//绑定Value属性
}
public void bindlistBox(ListBox lb, string sql)//绑定ListBox控件
{
DataTable dt = new DataTable();
dt=createSource(sql);
lb.DataSource = dt;
lb.DisplayMember = dt.Columns[1].ColumnName;
lb.ValueMember = dt.Columns[0].ColumnName;
}
private DataTable createSource(string sql)//生成DataTable对象
{
DataTable dt = new DataTable();
SqlDataReader dr;
db.runSql(sql, out dr);//执行sql语句
dt = db.ConvertDatareaderTable(dr);//转换成DataTable对象
return dt;
}
public void bindDgv(DataGridView gv, string sql)//绑定DataGridView控件
{
gv.DataSource = createSource(sql);
}
public void cbbIndexByText(ComboBox cbb, string value)//如果ConboBox的项集合中包含指定值,则使指定索引指向该值得索引
{
for (int i = 0; i < cbb.Items.Count; i++)//遍历ConboBox的项
{
if (cbb.Items[i].ToString() == value)
{
cbb.SelectedIndex = i;//选择索引
break;
}
}
}
}
}</span>