封装的ADO.NET对数据库操作经典类

系统 1644 0
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace LTP.SQLServerDAL
... {
/**/ /// <summary>
/// ADO.NET数据库操作基础类。
/// </summary>

public abstract class DbManagerSQL
... {
// 数据库连接字符串
protected static string connectionString = ConfigurationSettings.AppSettings[ " ConnectionString " ];
public DbManagerSQL()
... {
//
// TODO:在此处添加构造函数逻辑
//
}

/**/ /// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <paramname="SQLString"></param>
/// <returns></returns>

public static int ExecuteSql( string SQLString)
... {
using (SqlConnectionconnection = new SqlConnection(connectionString))
... {
using (SqlCommandcmd = new SqlCommand(SQLString,connection))
... {
try
... {
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}

catch (System.Data.SqlClient.SqlExceptionE)
... {
throw new Exception(E.Message);
}

}

}

}

/**/ /// <summary>
/// 执行两条SQL语句,实现数据库事务。
/// </summary>
/// <paramname="SQLString1"></param>
/// <paramname="SQLString2"></param>

public static void ExecuteSqlTran( string SQLString1, string SQLString2)
... {
using (SqlConnectionconnection = new SqlConnection(connectionString))
... {
connection.Open();
SqlCommandcmd
= new SqlCommand();
cmd.Connection
= connection;
SqlTransactiontx
= connection.BeginTransaction();
cmd.Transaction
= tx;
try
... {
cmd.CommandText
= SQLString1;
cmd.ExecuteNonQuery();
cmd.CommandText
= SQLString2;
cmd.ExecuteNonQuery();
tx.Commit();
}

catch (System.Data.SqlClient.SqlExceptionE)
... {
tx.Rollback();
throw new Exception(E.Message);
}

finally
... {
cmd.Dispose();
connection.Close();
}

}

}

/**/ /// <summary>
/// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
/// </summary>
/// <paramname="SQLStringList"></param>

public static void ExecuteSqlTran( string SQLStringList)
... {
using (OdbcConnectionconn = new OdbcConnection(connectionString))
... {
conn.Open();
OdbcCommandcmd
= new OdbcCommand();
cmd.Connection
= conn;
OdbcTransactiontx
= conn.BeginTransaction();
cmd.Transaction
= tx;
try
... {
string []split = SQLStringList.Split( new Char[] ... { ' ; ' } );
foreach ( string strsql in split)
... {
if (strsql.Trim() != "" )
... {
cmd.CommandText
= strsql;
cmd.ExecuteNonQuery();
}

}

tx.Commit();
}

catch (System.Data.Odbc.OdbcExceptionE)
... {
tx.Rollback();
throw new Exception(E.Message);
}

}

}

/**/ /// <summary>
/// 执行带一个存储过程参数的的SQL语句。
/// </summary>
/// <paramname="SQLString"></param>
/// <paramname="content"></param>
/// <returns></returns>

public static int ExecuteSql( string SQLString, string content)
... {
using (SqlConnectionconnection = new SqlConnection(connectionString))
... {
SqlCommandcmd
= new SqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter
= new System.Data.SqlClient.SqlParameter( " @content " ,SqlDbType.NText);
myParameter.Value
= content;
cmd.Parameters.Add(myParameter);
try
... {
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}

catch (System.Data.SqlClient.SqlExceptionE)
... {
throw new Exception(E.Message);
}

finally
... {
cmd.Dispose();
connection.Close();
}

}

}

/**/ /// <summary>
/// 向数据库里插入图像格式的字段
/// </summary>
/// <paramname="strSQL"></param>
/// <paramname="fs"></param>
/// <returns></returns>

public static int ExecuteSqlInsertImg( string strSQL, byte []fs)
... {
using (SqlConnectionconnection = new SqlConnection(connectionString))
... {
SqlCommandcmd
= new SqlCommand(strSQL,connection);
System.Data.SqlClient.SqlParametermyParameter
= new System.Data.SqlClient.SqlParameter( " @fs " ,SqlDbType.Image);
myParameter.Value
= fs;
cmd.Parameters.Add(myParameter);
try
... {
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}

catch (System.Data.SqlClient.SqlExceptionE)
... {
throw new Exception(E.Message);
}

finally
... {
cmd.Dispose();
connection.Close();
}


}

}

/**/ /// <summary>
/// 执行一条计算查询结果语句,返回查询结果(整数)。
/// </summary>
/// <paramname="strSQL"></param>
/// <returns></returns>

public static int GetCount( string strSQL)
... {
using (SqlConnectionconnection = new SqlConnection(connectionString))
... {
SqlCommandcmd
= new SqlCommand(strSQL,connection);
try
... {
connection.Open();
SqlDataReaderresult
= cmd.ExecuteReader();
int i = 0 ;
while (result.Read())
... {
i
= result.GetInt32( 0 );
}

result.Close();
return i;
}

catch (System.Data.SqlClient.SqlExceptione)
... {
throw new Exception(e.Message);
}

finally
_5044_5095_Closed_Tex
分享到:
评论

封装的ADO.NET对数据库操作经典类


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论