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 中設定
String> "DBString"].ConnectionString;
///
/// 無事務,資料查詢
///
/// 儲存過程名稱或Sql語句內容 // 參數清單
///
public static int Execute,Query(Icoter); [] CommandParams)
{
SqlCommand cmd = new SqlCommand();
Sql# conn = new SqlConnection(ConnectString);
Sql# PrepareCommand( cmd, conn, null, cmdType, cmdText, CommandParams);
int val = cmd.ExecuteNonQuery(); " . return val;
}
catch
{
throw;
conn.Close();
}
##}
///
///
///
/// / ?預存程序名稱或Sql語句
/// 參數>參數
# >參數# ;/returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
PrepareCommand( cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
/*if (cmdType == CommandType.StoredProcedure)
Add("@RETURN_VALUE", "") .Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
##val = (int)cmd. else
if (cmdType==CommandType.Text)
{
中 c int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear ();
return val;
}
#
///
/// 回傳資料集 DataReader
///
///
///
///
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
連線conn = new SqlConnection(ConnectString);
try
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);# 311 cmo ; ader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
conn. Close();
// 拋出新的例外("作業失敗!");
throw new Exception(ex.Message);
throw new Exception(ex.Message);
}
/// summary>
///
/// ## "cmdText">
/// ///
# S. trans.Connection, trans, cmdType, cmdText, CommandParams) ;
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();## 返回}
公共靜態物件ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
# SflCommand cm ##try
{
PrepareCommand(cmd, conn, null, cm , cmd ();
cmd.Parameters.Clear();
return val;
}
catch#
}
{
##}
public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
> PrepareCommand(cmd, trans.Connection, trans , cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
#cmd.Parameters.Clear() }
/// 依照Sql語句取得表格
/////// name="cm/ name="cmType /param>
/// ///
//
/returns>
public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{## ; d = new SqlCommand( );
SqlConnection conn = new SqlConnection(ConnectString);
try
{
# {
conn.Close();
# # public static DataTable執行表(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
DataTable temptable = new DataTable();#卷trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);SlCommand; Fill(temptable);
cmd.Parameters.Clear( );
return temptable;
##}
///
///
/// < / ///
public static DataSet ExecuteData( # {
SqlConnection conn = new SqlConnection(ConnectString);
SqlCommand cmd = new SqlCommand();
#DataSetSet,Data #Data ## {
{
# PrepareCommand(cmd, conn, null, cmdType, cm , Commandams);## 頫Adapter(cmd);
sqlAdapter.Fill(TempDataSet);
返回TempDataSet;
}
最後 # }
}
公共靜態資料集ExecuteDataSet(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
DataSet TempDataSet = new DataSet ( );
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter qpDataAd sqlAdapter.Fill(TempDataSet);
cmd. Parameters .Clear();
return TempDataSet;
}
///
## ///
///
// ///
/// private static void PreparationCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandTypedType, string cmd, Sstr .
##if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cm #) # ##if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
# cmd foreach (cmdParms 中的SqlParameter parm)
# {
返回值== null ? DBNull.Value : value;
#公共靜態物件 FromDBValue(object dbValue)
# db {
# db == null : dbValue; }
}
以上是關於.net C# Sql資料庫SQLHelper類別實例代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!