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

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

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

}
//数据库连接参数
public SqlConnection DataConn()
{
return myConnection;
}
//开始一个事务
public void BeginTransaction()
{
if ((myConnection == null) || (myConnection.State == ConnectionState.Closed))
{
return;
}
else
{
Transaction = myConnection.BeginTransaction();
IsInTransaction = true;
}
}
//提交一个事务
public void CommitTransaction()
{
if (Transaction != null)
{
Transaction.Commit();
IsInTransaction = false;
Transaction = null;
myConnection.Close();
}
else
{
return;
}
}
//回滚一个事务
public void RollbackTransaction()
{
if (Transaction != null)
{
Transaction.Rollback();
IsInTransaction = false;
Transaction = null;
myConnection.Close();
}
else
{
return;
}
}

public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
openLink();
this.strSQL = tempStrSQL;
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
myConnection.Close();
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
openLink();
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
myConnection.Close();
return changedDataSet;//返回更新了的数据库表
}
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
openLink();
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
myConnection.Close();
return tempDataSet.Tables[0];
}
//返回单个字符
public string GetDataChar(string tempStrSQl, string tempReturnChar)
{
openLink();
cmd = new SqlCommand(tempStrSQl, myConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string returnChar = dr[tempReturnChar].ToString();
myConnection.Close();
return returnChar;
}
myConnection.Close();
return " ";
}
//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
openLink();
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;

文章评论

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