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

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

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

if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}
}


//返回查询结果的SqlExecuteScalar
public static object SqlExecuteScalar(SqlCommand objCommand)
{
SqlConnection objConnection=new SqlConnection(strConnect);
objCommand.Connection=objConnection;
try
{
if(objConnection.State==ConnectionState.Closed)
{
objConnection.Open();
}
return objCommand.ExecuteScalar();
}
catch(Exception e)
{
throw e;
}
finally
{
if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}
}
//重写SqlExecuteScalar
public static object SqlExecuteScalar(string strCmd)
{
SqlCommand objCommand=new SqlCommand();
objCommand.CommandText=strCmd;
return SqlExecuteScalar(objCommand);
}

//返回DataSet
public static DataSet GetDataSet(string strCmd,string TableName)
{
SqlConnection objConnection=new SqlConnection(strConnect);
SqlCommand objCommand=new SqlCommand(strCmd,objConnection);
try
{
if(objConnection.State==ConnectionState.Closed)
{
objConnection.Open();
}
DataSet objDs=new DataSet();
SqlDataAdapter objDa=new SqlDataAdapter(objCommand);
objDa.Fill(objDs,TableName);
return objDs;
}
catch(Exception e)
{
throw e;
}
finally
{
if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}

}
public DBUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}

------解决方案--------------------
其实微软的企业库是一个不错的选择,如果楼主真的要学习但又不想看它的英文注释的话可以去书店找一下,上次我有看到一本专门写企业库的书,不过不记得书名了,只能帮你这么多了:)
------解决方案--------------------
自己写的,也不知道能不能用上
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace LY
{
class C_Public
{
private string strSQL;
private SqlConnection myConnection = null;
private SqlCommand cmd;
private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
private static SqlTransaction Transaction = null; //数据库事务
private static bool IsInTransaction = false; //是否在事务中
//程序用到的存储数据变量
public class AppNeddInfo
{
public static string psWorker;
}
public C_Public()
{
}
private void openLink()
{
string ConnectionText; //Store The Connection Sentence for Database
if ((myConnection == null) || (myConnection.State == ConnectionState.Closed))
{
ConnectionText = ReadConn();
myConnection = new SqlConnection(ConnectionText);
try
{
myConnection.Open();//Open Database
}
catch
{
myConnection = null;
}
}
}
public string ReadConn()
{
string str = "server=FYGS-X;database=text;uid=sa;pwd=sa ";
return str;

文章评论

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