SqlCommon类简介
SqlCommon类是我在工作中逐步完善的一个用C#编写的基于 .Net Framework 2.0/3.0/3.5 的一个 SQL Server 2000/2005/2008 访问类。以简单的手段实现了数据库的访问,并且可以根据参数名获得所执行的存储过的传出参数的值。可以为刚刚接触编程ADO.NET编程的开发人员做一个简单的参考。
SqlCommon类源代码
SqlCommon使用手册
构造函数
SqlCommon(string connectionStrings, CommandType sqlCommandType) | SqlCommon构造函数,不启用执行异常日志记录 |
SqlCommon(string connectionStrings, CommandType sqlCommandType, bool LogSqlException) | SqlCommon构造函数,可以设置启用执行异常日志记录 |
属性
Command | SqlCommand |
CommandText | 需要执行的Sql语句或存储过程名 |
Connection | 返回连接对象 |
CountAllTableRows | 统计所有返回表中的行数之和 |
CountTables | 统计返回表的数目 |
ErrorMessage | 错误消息 |
ErrorSource | 错误源 |
ErrorStackTrace | 错误堆栈跟踪信息 |
ExecuteRight | sql语句是否执行正确 |
方法
AddParameter(string parameterName, object value) | 为SqlCommon对象增加参数,Direction Input |
AddParameter(string parameterName, ParameterDirection parameterDirection, SqlDbType parmeterType, int parmeterLength) | 为SqlCommon对象增加参数,out和Return参数 |
ClearParameter() | 清空SqlCommon对象中的所有参数 |
ExecuteSql() | 执行SQL语句或存储过程 |
GetParameterValue(string parameterName) | 得到参数的值 |
RemoveParameter(string parameterName) | 从SqlCommon对象中移除参数 |
使用说明
建立SqlCommon对象
1. SqlCommon.SqlCommon mySqlCommon = new SqlCommon.SqlCommon([数据库连接字符串], [要执行的SQL命令类型(存储过程CommandType.StoredProcedure或SQL语句CommandType.Text)]);
2. SqlCommon.SqlCommon mySqlCommon = new SqlCommon.SqlCommon([数据库连接字符串], [要执行的SQL命令类型], [是否在本地文件中记录SQL执行异常(true记录/false不记录默认不记录)]);
确定需要执行的存储过程或SQL命令
mySqlCommon.CommandText = "test1"; //test1为存储过程名
mySqlCommon.CommandText = "SELECT bomno FROM bomhead WHERE bomnotxt = @bomnotxt"; //@bomnotxt 为在语句中定义的输入变量。
添加参数
mySqlCommon.AddParameter("@bomnotxt",tbBomnotxt.Text); //添加正常存储过程输入参数或SQL语句参数
mySqlCommon.AddParameter("@bomno", ParameterDirection.Output,SqlDbType.NVarChar,20); //添加存储过程输出参数 ParameterDirection.Output 为参数类型,SqlDbType.NVarChar为输出参数的值类型不可省略,20输出参数的值长度不可省略
mySqlCommon.AddParameter("@return", ParameterDirection.ReturnValue,SqlDbType.Int,10); //添加存储过程return参数 ParameterDirection.ReturnValue 为参数类型,SqlDbType.Int为return参数的值类型不可省略,10return参数的值长度不可省略
添加执行
DataSet ds = mySqlCommon.ExecuteSql(); //申明DataSet接收返回DataSet
mySqlCommon.GetParameterValue("@bomno"); //得到输出参数@bomno的输出值
mySqlCommon.GetParameterValue("@return"); //得到return参数@return的输出值
调用SqlCommon的一个例子
调用存储过程
SqlCommon.SqlCommon mySqlCommon = new SqlCommon.SqlCommon(WebConfigurationManager.ConnectionStrings["minipdm"].ConnectionString, CommandType.StoredProcedure);mySqlCommon.CommandText = "test1";
mySqlCommon.AddParameter("@bomnotxt",tbBomnotxt.Text); //添加参数
mySqlCommon.AddParameter("@bomno", ParameterDirection.Output,SqlDbType.NVarChar,20); //添加存储过程输出参数
mySqlCommon.AddParameter("@return", ParameterDirection.ReturnValue,SqlDbType.Int,10); //添加存储过程return参数
DataSet ds = new DataSet();
ds = mySqlCommon.ExecuteSql();
Label1.Text = "TableBomno: " + ds.Tables[0].Rows[0]["bomno"].ToString();
Label2.Text = "OutputBomno: " + mySqlCommon.GetParameterValue("@bomno");
Label3.Text = "ReturnState: " + mySqlCommon.GetParameterValue("@return");
直接执行语句
SqlCommon.SqlCommon mySqlCommon = new SqlCommon.SqlCommon(WebConfigurationManager.ConnectionStrings["minipdm"].ConnectionString, CommandType.Text);mySqlCommon.CommandText = "SELECT bomno FROM bomhead WHERE bomnotxt = @bomnotxt";
mySqlCommon.AddParameter("@bomnotxt", tbBomnotxt.Text); //添加参数
DataSet ds = new DataSet();
ds = mySqlCommon.ExecuteSql();
Label1.Text = "TableBomno: " + ds.Tables[0].Rows[0]["bomno"].ToString();