關於.net C# Sql資料庫SQLHelper類執行個體代碼

來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public class SQLHelper
{
//取得資料庫連接web.config 中配置
public static readonly string ConnectString = ConfigurationManager.ConnectionStrings["DBString"].ConnectionString;

/// <summary>
/// 無事務,資料查詢
/// </summary>
/// <param name="cmdType">預存程序或Sql語句</param>
/// <param name="cmdText">預存程序名或Sql語句內容</param>
/// <param name="CommandParams">參數列表</param>
/// <returns></returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection(ConnectString);

try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch
{
throw;
}
finally
{
conn.Close();

}

}


/// <summary>
/// 有事務,資料操作類
/// </summary>
/// <param name="trans">事務</param>
/// <param name="cmdType">操作類別 (stored procedure,sql)</param>
/// <param name="cmdText">預存程序名或Sql語句</param>
/// <param name="CommandParams">參數</param>
/// <returns>返回影響的資料行數</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{

SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
/*if (cmdType == CommandType.StoredProcedure)
{
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();

val = (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
else
if (cmdType==CommandType.Text)
{
val = cmd.ExecuteNonQuery();
}*/
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();

return val;
}

/// <summary>
/// 返回資料集 DataReader
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);

try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
// throw new Exception("操作失敗!");
throw new Exception(ex.Message);
}
}

/// <summary>
/// 有事務的取資料
/// </summary>
/// <param name="trans"></param>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}

public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection(ConnectString);

try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch
{
throw;
}
finally
{
conn.Close();
}

}

public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;

}

/// <summary>
/// 根據Sql語句取得表
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable temptable = new DataTable();

SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection(ConnectString);

try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);

SqlDataAdapter da = new SqlDataAdapter(cmd);

SqlCommandBuilder scb = new SqlCommandBuilder(da);

da.Fill(temptable);
}
finally
{
conn.Close();
}

return temptable;
}


public static DataTable ExecuteTable(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable temptable = new DataTable();
SqlCommand cmd = new SqlCommand();

PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(temptable);
cmd.Parameters.Clear();

return temptable;

}


/// <summary>
/// 根據Sql語句或預存程序取得資料
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlConnection conn = new SqlConnection(ConnectString);

SqlCommand cmd = new SqlCommand();

DataSet TempDataSet = new DataSet();

try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameters.Clear();
return TempDataSet;

}
finally
{
conn.Close();
}

}


public static DataSet ExecuteDataSet(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
DataSet TempDataSet = new DataSet();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameters.Clear();
return TempDataSet;
}

/// <summary>
/// 產生Sql語句或準備
/// </summary>
/// <param name="cmd"></param>
/// <param name="conn"></param>
/// <param name="trans"></param>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="cmdParms"></param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{

if (conn.State != ConnectionState.Open)
conn.Open();

cmd.Connection = conn;
cmd.CommandText = cmdText;

if (trans != null)
cmd.Transaction = trans;

cmd.CommandType = cmdType;

if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}

public static object ToDBValue(object value)
{
return value == null ? DBNull.Value : value;
}

public static object FromDBValue(object dbValue)
{
return dbValue == DBNull.Value ? null : dbValue;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.