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
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
发表评论
评论