MyException - 我的异常网
当前位置:我的异常网» C# » 求1性能较好的c#数据库操作类

求1性能较好的c#数据库操作类(4)

www.MyException.Cn  网友分享于:2013-12-20  浏览:45次

/// 当前待处理事务提交,失败全部回滚
/// <returns> 成功提交返回true </returns>
public bool Commit()
{
//如果没有开启事务处理功能,不做任何操作,直接返回成功
if (!m_bTransaction)
{
return true;
}

try
{
m_sqlTransaction.Commit();
}
catch(SqlException e)
{
m_sqlTransaction.Rollback();
throw e;
}
return true;
}

/// <summary>
/// 除去对象资源.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}

/// <summary>
/// 释放对象实例变量.
/// </summary>
protected virtual void Dispose(
bool bDispose)
{
if (!bDispose)
return;

if (m_bConnected)
{
if (m_sqlConnection.State != ConnectionState.Closed )
{
m_sqlConnection.Dispose();
m_sqlConnection.Close();

m_sqlCommand = null;
m_sqlConnection = null;
m_sqlTransaction = null;

m_bConnected = false;
}
}
}
}
}

------解决方案--------------------
SQLHElper执行的都是什么存储过程,有执行SQL语句的没有,比如删除,更新等SQL单一的语句,而不是存储过程
---------------------------
谁说没有?这个不是吗?!!!

public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
}
------解决方案--------------------
int rowEffect=SqlHelper.ExecuteNonQuery( "constr ",CommandType.Text, "sql ");
就可以了
------解决方案--------------------
我这里有个简单的

using System;
using System.Data.SqlClient;
using System.Data;

namespace 数据报表模块
{
/// <summary>
/// DBUtil 的摘要说明。
/// </summary>
public class DBUtil
{
private static string strConnect =
System.Configuration.ConfigurationSettings.AppSettings[ "connStr "];


public static int SqlExecuteNonQuery(SqlCommand objCommand)
{
SqlConnection objConnection=new SqlConnection(strConnect);
objCommand.Connection=objConnection;
try
{
if(objConnection.State==ConnectionState.Closed)
{
objConnection.Open();
}
return objCommand.ExecuteNonQuery();
}
catch(Exception e)
{
throw e;
}
finally
{
if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}
}
//重写SqlExecuteNonQuery,这样写以后调用类时只用写SQL命令
public static int SqlExecuteNonQuery(string strCmd)
{
SqlCommand objCommand=new SqlCommand(strCmd);
//调用已定义的方法
return SqlExecuteNonQuery(objCommand);
}


//带返回结果的SqlExecuteReader,不采用重写的方法
public static SqlDataReader SqlExecutrReader(string strCmd)
{
SqlConnection objConnection=new SqlConnection(strConnect);
SqlCommand objCommand=new SqlCommand(strCmd,objConnection);
try
{
if(objConnection.State==ConnectionState.Closed)
{
objConnection.Open();
}
return objCommand.ExecuteReader();
}
catch(Exception e)
{
throw e;
}
finally
{

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有