解决方案 »
- winform智能开发,没有WebClient怎么办?
- ACReport报表控件的最新版本(含web版控件)
- Could not find file 'C:\WINDOWS\TEMP\5ko7gm2e.dll'.
- 请教关于引用WindowsAPI函数的问题
- 关于IIS404页面的设置
- 求Beginning C# 源码
- WebBroswer控件Win8触屏缩放问题
- 在窗体上播放的FALSH如何与主程序进行交互?
- 关于ReadLine()的问题,来者有分!
- 怎么新建一个.cs文件
- 如何获取本机outlook中的待办事项信息,信息
- winfrom中显示word(2007/2010) 并且支持添加书签和定位书签 怎么做呢。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;public string connectionString
{
get;
private set;
}public DBOperator(string connectionString)
{
this.connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
}
public DataTable GetDataTable(SqlCommand sqlCommand)
{
ChangeNullToDBNullValue(sqlCommand);
bool useDefaultConnection = false;
if (sqlCommand.Connection == null)
{
useDefaultConnection = true;
sqlCommand.Connection = new SqlConnection(this.connectionString);
}
else
{
useDefaultConnection = false;
if (sqlCommand.Connection.State != ConnectionState.Closed)
{
throw new ArgumentException("SqlCommand's connection state must be closed.");
}
} sqlCommand.Connection.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = sqlCommand;
try
{
da.Fill(dt);
}
catch (Exception ex)
{
DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
}
sqlCommand.Connection.Close();
return dt;
}public ExecuteResult ExecuteNonQuery(SqlCommand sqlCommand)
{
ChangeNullToDBNullValue(sqlCommand);
bool useDefaultConnection = false;
if (sqlCommand.Connection == null)
{
useDefaultConnection = true;
sqlCommand.Connection = new SqlConnection(this.connectionString);
}
else
{
useDefaultConnection = false;
if (sqlCommand.Connection.State != ConnectionState.Closed)
{
throw new ArgumentException("SqlCommand's connection state must be closed.");
}
}
sqlCommand.Connection.Open();
sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
try
{
int rows = sqlCommand.ExecuteNonQuery();
sqlCommand.Transaction.Commit();
return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = rows };
}
catch (SqlException ex)
{
sqlCommand.Transaction.Rollback();
DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
}
}public ExecuteResult ExecuteScalar(SqlCommand sqlCommand)
{
ChangeNullToDBNullValue(sqlCommand);
bool useDefaultConnection = false;
if (sqlCommand.Connection == null)
{
useDefaultConnection = true;
sqlCommand.Connection = new SqlConnection(this.connectionString);
}
else
{
useDefaultConnection = false;
if (sqlCommand.Connection.State != ConnectionState.Closed)
{
throw new ArgumentException("SqlCommand's connection state must be closed.");
}
}
sqlCommand.Connection.Open();
sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
try
{
object returnValue = sqlCommand.ExecuteScalar();
sqlCommand.Transaction.Commit();
return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = returnValue };
}
catch (SqlException ex)
{
sqlCommand.Transaction.Rollback();
DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
}
}
private void ChangeNullToDBNullValue(SqlCommand command)
{
foreach (SqlParameter para in command.Parameters)
{
if (para.Value == null)
{
para.Value = DBNull.Value;
}
else
{
if (para.Value is string)
{
if (string.IsNullOrWhiteSpace(para.Value.ToString()))
{
para.Value = string.Empty;
}
}
}
}
}