数据访问类SqlClient
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;
namespace Sun.WebExam
{
public class DB : IDisposable
{
// 连接数据源
private SqlConnection con;
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="dataReader">返回存储过程返回值</param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
//return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <param name="dataReader">存储过程所需参数</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
//return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回SqlCommand对象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 确认打开连接
Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 依次把参数传入存储过程
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 加入返回参数
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
/// <summary>
/// 执行一条不需要返回值SQL命令,比如插入、删除
/// </summary>
/// <param name="commtext"></param>
public void ExecCommand(string commtext)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
/// <summary>
/// 执行多条不需要返回值SQL命令,比如插入、删除
/// </summary>
/// <param name="commtext">多条SQL语句</param>
public void ExecCommand(string[] commtext)
{
Open();
for(int i=0;i<commtext.Length;i++)
{
SqlCommand cmd = new SqlCommand(commtext[i],con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
/// <summary>
/// 执行一条不需要返回值SQL命令,比如插入、删除,可以带一个参数数组
/// </summary>
/// <param name="commtext"></param>
/// <param name="prams"></param>
public void ExecCommand(string commtext,SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
/// <summary>
/// 执行一条SQL语句,把图片保存到数据库
/// </summary>
/// <param name="commtext">sql语句</param>
/// <param name="content">byte[]</param>
// public void ExecCommand(string commtext,byte[] content)
// {
// Open();
// SqlCommand cmd=new SqlCommand(commtext,con);
// cmd.CommandType=CommandType.Text;
// cmd.Parameters.Add("@pic",SqlDbType.Image).Value=content;
// cmd.ExecuteNonQuery();
// cmd.Dispose();
// }
/// <summary>
/// 执行一条SQL语句,把DataSet中的字段(图片)保存到数据库
/// </summary>
/// <param name="commtext"></param>
/// <param name="content"></param>
public void ExecCommand(string commtext,object content)
{
Open();
SqlCommand cmd=new SqlCommand(commtext,con);
cmd.CommandType=CommandType.Text;
cmd.Parameters.Add("@pic",content);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
/// <summary>
/// 执行一条统计类的SQL语句,返回一个整数
/// </summary>
/// <param name="CommText"></param>
/// <returns></returns>
public int ExecScalar(string CommText)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
return cmd.ExecuteScalar()!=null?(int)cmd.ExecuteScalar():0;
}
/// <summary>
/// 执行一条统计类的SQL语句,返回一个整数。可以带参数数字
/// </summary>
/// <param name="CommText"></param>
/// <param name="prams"></param>
/// <returns></returns>
public int ExecScalar(string CommText,SqlParameter[] prams)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
return cmd.ExecuteScalar()!=null?(int)cmd.ExecuteScalar():0;
}
/// <summary>
/// 查询最大值
/// </summary>
/// <param name="CommText"></param>
/// <returns></returns>
public string SelectMax(string CommText)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
return cmd.ExecuteScalar().ToString();
}
/// <summary>
/// 执行一条查询语句,并将查询结果返回为一个数据读器;
/// </summary>
/// <param name="commtext"></param>
/// <param name="dr"></param>
public void ExecQuery(string commtext,out SqlDataReader dr)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
dr=cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Dispose();
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
// 打开数据库连接
if (con == null)
{
// RegistryKey rk=Registry.LocalMachine;
// RegistryKey subrk=rk.OpenSubKey("software\\Sunsoft\\SunCdas");
// string connString=(string)subrk.GetValue("server");
string connString="workstation id=xxx;packet size=4096;user id=sa;pwd=;data source=syringa;persist security info=False;initial catalog=SunWebExam";
con = new SqlConnection(connString);
con.Open();
}
if(con.State ==System.Data.ConnectionState.Closed)
con.Open();
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 确认连接是否已经关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
/// <summary>
/// 传入输入参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param></param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeReturnParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
}
/// <summary>
/// 生成存储过程参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方向</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
/// <summary>
/// 执行一条SQL语句,并返回一个命名了数据表的数据集
/// </summary>
/// <param name="cmdtext">SQL语句</param>
/// <param name="tablename">表名</param>
/// <param name="ds">数据集</param>
public void Exec4DS(string CmdText,string TableName,out DataSet DS)
{
Open();
DS=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(CmdText,con);
try
{
da.Fill(DS,TableName);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 执行一条带参数的SQL语句,并返回一个命名了数据表的数据集
/// </summary>
/// <param name="cmdtext">SQL语句</param>
/// <param name="tablename">表名</param>
/// <param name="prams">参数数组</param>
/// <param name="ds">数据集</param>
public void Exec4DS(string cmdtext,string tablename,SqlParameter[] prams,out DataSet ds)
{
Open();
ds=new DataSet();
SqlCommand cmd = new SqlCommand(cmdtext, con);
cmd.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(ds,tablename);
}
/// <summary>
/// 一次执行多条SQL语句,将多个表填入一个DataSet
/// </summary>
/// <param name="sqlCmds">SQL语句数组</param>
/// <param name="TableNames">表名数组</param>
/// <param name="DS">数据集名</param>
public void ExecXDS(string[] sqlCmds,string[] TableNames,out DataSet DS)
{
Open();
DS=new DataSet();
for(int i=0;i<sqlCmds.Length;i++)
{
SqlDataAdapter da=new SqlDataAdapter(sqlCmds[i],con);
da.Fill(DS,TableNames[i]);
}
}
}
}
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;
namespace Sun.WebExam
{
public class DB : IDisposable
{
// 连接数据源
private SqlConnection con;
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="dataReader">返回存储过程返回值</param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
//return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <param name="dataReader">存储过程所需参数</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
//return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回SqlCommand对象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 确认打开连接
Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 依次把参数传入存储过程
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 加入返回参数
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
/// <summary>
/// 执行一条不需要返回值SQL命令,比如插入、删除
/// </summary>
/// <param name="commtext"></param>
public void ExecCommand(string commtext)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
/// <summary>
/// 执行多条不需要返回值SQL命令,比如插入、删除
/// </summary>
/// <param name="commtext">多条SQL语句</param>
public void ExecCommand(string[] commtext)
{
Open();
for(int i=0;i<commtext.Length;i++)
{
SqlCommand cmd = new SqlCommand(commtext[i],con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
/// <summary>
/// 执行一条不需要返回值SQL命令,比如插入、删除,可以带一个参数数组
/// </summary>
/// <param name="commtext"></param>
/// <param name="prams"></param>
public void ExecCommand(string commtext,SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
/// <summary>
/// 执行一条SQL语句,把图片保存到数据库
/// </summary>
/// <param name="commtext">sql语句</param>
/// <param name="content">byte[]</param>
// public void ExecCommand(string commtext,byte[] content)
// {
// Open();
// SqlCommand cmd=new SqlCommand(commtext,con);
// cmd.CommandType=CommandType.Text;
// cmd.Parameters.Add("@pic",SqlDbType.Image).Value=content;
// cmd.ExecuteNonQuery();
// cmd.Dispose();
// }
/// <summary>
/// 执行一条SQL语句,把DataSet中的字段(图片)保存到数据库
/// </summary>
/// <param name="commtext"></param>
/// <param name="content"></param>
public void ExecCommand(string commtext,object content)
{
Open();
SqlCommand cmd=new SqlCommand(commtext,con);
cmd.CommandType=CommandType.Text;
cmd.Parameters.Add("@pic",content);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
/// <summary>
/// 执行一条统计类的SQL语句,返回一个整数
/// </summary>
/// <param name="CommText"></param>
/// <returns></returns>
public int ExecScalar(string CommText)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
return cmd.ExecuteScalar()!=null?(int)cmd.ExecuteScalar():0;
}
/// <summary>
/// 执行一条统计类的SQL语句,返回一个整数。可以带参数数字
/// </summary>
/// <param name="CommText"></param>
/// <param name="prams"></param>
/// <returns></returns>
public int ExecScalar(string CommText,SqlParameter[] prams)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
return cmd.ExecuteScalar()!=null?(int)cmd.ExecuteScalar():0;
}
/// <summary>
/// 查询最大值
/// </summary>
/// <param name="CommText"></param>
/// <returns></returns>
public string SelectMax(string CommText)
{
Open();
SqlCommand cmd=new SqlCommand(CommText,con);
cmd.CommandType=CommandType.Text;
return cmd.ExecuteScalar().ToString();
}
/// <summary>
/// 执行一条查询语句,并将查询结果返回为一个数据读器;
/// </summary>
/// <param name="commtext"></param>
/// <param name="dr"></param>
public void ExecQuery(string commtext,out SqlDataReader dr)
{
Open();
SqlCommand cmd = new SqlCommand(commtext,con);
cmd.CommandType = CommandType.Text;
dr=cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Dispose();
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
// 打开数据库连接
if (con == null)
{
// RegistryKey rk=Registry.LocalMachine;
// RegistryKey subrk=rk.OpenSubKey("software\\Sunsoft\\SunCdas");
// string connString=(string)subrk.GetValue("server");
string connString="workstation id=xxx;packet size=4096;user id=sa;pwd=;data source=syringa;persist security info=False;initial catalog=SunWebExam";
con = new SqlConnection(connString);
con.Open();
}
if(con.State ==System.Data.ConnectionState.Closed)
con.Open();
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 确认连接是否已经关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
/// <summary>
/// 传入输入参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param></param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeReturnParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
}
/// <summary>
/// 生成存储过程参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方向</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
/// <summary>
/// 执行一条SQL语句,并返回一个命名了数据表的数据集
/// </summary>
/// <param name="cmdtext">SQL语句</param>
/// <param name="tablename">表名</param>
/// <param name="ds">数据集</param>
public void Exec4DS(string CmdText,string TableName,out DataSet DS)
{
Open();
DS=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(CmdText,con);
try
{
da.Fill(DS,TableName);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 执行一条带参数的SQL语句,并返回一个命名了数据表的数据集
/// </summary>
/// <param name="cmdtext">SQL语句</param>
/// <param name="tablename">表名</param>
/// <param name="prams">参数数组</param>
/// <param name="ds">数据集</param>
public void Exec4DS(string cmdtext,string tablename,SqlParameter[] prams,out DataSet ds)
{
Open();
ds=new DataSet();
SqlCommand cmd = new SqlCommand(cmdtext, con);
cmd.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(ds,tablename);
}
/// <summary>
/// 一次执行多条SQL语句,将多个表填入一个DataSet
/// </summary>
/// <param name="sqlCmds">SQL语句数组</param>
/// <param name="TableNames">表名数组</param>
/// <param name="DS">数据集名</param>
public void ExecXDS(string[] sqlCmds,string[] TableNames,out DataSet DS)
{
Open();
DS=new DataSet();
for(int i=0;i<sqlCmds.Length;i++)
{
SqlDataAdapter da=new SqlDataAdapter(sqlCmds[i],con);
da.Fill(DS,TableNames[i]);
}
}
}
}