程序设计的一个重要思想是代码和组建的重用性,这样不仅可以提高程序的开发效率,减少程序员的代码工作量,不要让程序员变成敲代码的工具
重要的是体现面向对象的特征.为了这个目的我们往往建立个公共类抽出公共的方法为程序员留出变成接口,这样可以提高我们的开发效率.
呵呵,下面进入正题----刚接触asp.net时写的一个SqlHelper类,程序写的不是太好,适合初学者.....
================================================================
#region ================================================================
* FileName : Class1.cs
* functions :将数据库操作的信息都放到里面,实现代码重用和组件化编程
=================================================================*/
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
--引入命名空间
#region--引入命名空间
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
#endregion
namespace
DataBaseClassLibrary
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 将数据库操作的信息都放到里面,实现代码重用和组件化编程。
/// </summary>
public class Class1
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
---成员变量#region---成员变量
private SqlConnection myconnection= null;
private SqlCommand Mycommand = null;
private SqlDataAdapter da = null;
private DataSet ds = null;
private SqlDataReader DR = null;
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--设置属性,返回类内置成员#region--设置属性,返回类内置成员
//设置属性返回SqlConnection对象
public SqlConnection MySqlconnection
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return myconnection;
}
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
myconnection = value;
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
----构造函数重载#region----构造函数重载
//构造函数部分
public Class1(string database)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string Str = "server = .; database ="+ database+"; uid = sa; pwd = 123123";
MySqlconnection = new SqlConnection(Str);
}
public Class1(string server, string database, string uid, string pwd)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string Str = "server = "+server+"; database ="+database+"; uid ="+uid+"; pwd = "+pwd;
MySqlconnection = new SqlConnection(Str);
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--返回一个SqlCommand对象#region--返回一个SqlCommand对象
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 返回一个SqlCommand对象,Sql语句执行形式
/// </summary>
/// <param name="command">输入要执行的SqlCommand语句</param>
/// <returns>SqlCommand对象</returns>
public SqlCommand ReturnCommand(string command)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return (new SqlCommand(command,MySqlconnection));
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return null;
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--执行存储过程或SQL语句#region--执行存储过程或SQL语句
public int ExcuteCommands(string Strs)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int j=0;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Closed)
this.MySqlconnection.Open();
Mycommand = new SqlCommand(Strs,MySqlconnection);
j=Convert.ToInt32(Mycommand.ExecuteNonQuery());
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return 0;
}
return j;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--建立带参数的SqlCommand的执行过程,并调用存储过程#region--建立带参数的SqlCommand的执行过程,并调用存储过程
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 建立带参数的SqlCommand的执行过程,并调用存储过程
/// </summary>
/// <param name="ExcuteprocName">要执行的存储过程的名字</param>
/// <param name="pa">SqlParameter型的参数数组</param>
/// <returns>返回受影响的行数</returns>
public int ReturnCommand(string ExcuteprocName,params SqlParameter[] pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int j = 0;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Closed)
this.MySqlconnection.Open();
Mycommand = new SqlCommand(ExcuteprocName,MySqlconnection);
Mycommand.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter sp in pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Mycommand.Parameters .Add (sp);
}
j = (int)Mycommand.ExecuteNonQuery();
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return 0;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MySqlconnection.Close();
}
return j;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--返回SqlDataReader对象#region--返回SqlDataReader对象
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 返回SqlDataReader对象
/// </summary>
/// <param name="string">要执行的SqlCommand语句</param>
/// <returns>SqlDataReader对象</returns>
public SqlDataReader ReturnSqlDataReader(string command)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Closed)
this.MySqlconnection.Open();
Mycommand = new SqlCommand(command,MySqlconnection);
DR = Mycommand.ExecuteReader();
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//System.Web.HttpContext.Current.Response.Write(ex.Message);
return null;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MySqlconnection.Close();
}
return DR;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--返回DataSet对象#region--返回DataSet对象
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="command">SqlCommand对象</param>
/// <returns>DataSet对象</returns>
public DataSet ReturnDataSet(string Strcommand)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Mycommand = new SqlCommand(Strcommand,MySqlconnection);
da=new SqlDataAdapter(Mycommand);
ds = new DataSet();
da.Fill(ds,"table");
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//Response.Write(ex.Message);
return null;
}
return ds;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
---执行SQL语句或存储过程来填充DataGrid对象#region---执行SQL语句或存储过程来填充DataGrid对象
public void DataBings(string Str,DataGrid DataGrid1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlCommand Mycommand = new SqlCommand(Str,MySqlconnection);
SqlDataAdapter da = new SqlDataAdapter(Mycommand);
DataSet ds = new DataSet();
ds.Clear();
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--返回带参数的SqlCommand的执行过程的返回值#region--返回带参数的SqlCommand的执行过程的返回值
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 建立带参数的SqlCommand的执行过程,并调用存储过程
/// </summary>
/// <param name="ExcuteprocName">要执行的存储过程的名字</param>
/// <param name="pa">SqlParameter型的参数数组</param>
/// <returns>返回值</returns>
public int ReturnCommandvalue(string ExcuteprocName,params SqlParameter[] pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//int j=0;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Closed)
this.MySqlconnection.Open();
Mycommand = new SqlCommand(ExcuteprocName,MySqlconnection);
Mycommand.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter sp in pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Mycommand.Parameters .Add (sp);
}
SqlParameter ps = Mycommand.Parameters.Add("RETURN_VALUE",SqlDbType.Int);
ps.Direction = ParameterDirection.ReturnValue;
Mycommand.ExecuteNonQuery();
//j = (int)Mycommand.Parameters["RETURN_VALUE"].Value;
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return 0;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MySqlconnection.Close();
}
return (int)Mycommand.Parameters["RETURN_VALUE"].Value;;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--返回带参数的SqlCommand的执行过程的输出参数#region--返回带参数的SqlCommand的执行过程的输出参数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 建立带参数的SqlCommand的执行过程,并调用存储过程
/// </summary>
/// <param name="ExcuteprocName">要执行的存储过程的名字</param>
/// <param name="ps">输出参数</param>
/// <param name="pa">SqlParameter型的参数数组</param>
/// <returns>输出参数</returns>
public string OutputStoredProcedure(string ExcuteprocName,string ziduan ,string SqlDbTypes, params SqlParameter[] pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Closed)
this.MySqlconnection.Open();
Mycommand = new SqlCommand(ExcuteprocName,MySqlconnection);
Mycommand.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter sp in pa)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Mycommand.Parameters .Add (sp);
}
SqlParameter ps = Mycommand.Parameters.Add("@"+ziduan,SqlDbTypes);
ps.Size = 20;
ps.Direction = ParameterDirection.Output;
Mycommand.ExecuteNonQuery();
MySqlconnection.Close();
return Mycommand.Parameters["@"+ziduan].Value.ToString();
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return null;
}
}
#endregion
// #region---返回视图
// public DataView ReturnDataView()
// {
// }
// #endregion
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
--检查数据连接是否为关闭->关闭连接#region--检查数据连接是否为关闭->关闭连接
public void CloseConnection()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(this.MySqlconnection.State == ConnectionState.Open)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.MySqlconnection.Close();
}
}
#endregion
}
}