System 사용;
System.Collections.Generic 사용;
System.Text 사용;
System.Collections 사용;
System.Data.SqlClient 사용;
System.Data 사용;
System.Configuration 사용;
공개 클래스 SQLHelper取 {
// web.config에서 데이터베이스 연결을 얻습니다.
Public Static Readonly String ConnectString = ConfigurationManager.ConnectionStrings ["dbstring"] ///// lt; 트랜잭션 없음, 데이터 쿼리
; SQL 문 내용
| = cmd. |
}}}///// & lt; 요약 & gt;
/// 트랜잭션, 데이터 연산 클래스가 있습니다
/// & lt;/summary & gt ;
/// & lt; /param>
& lt;/param & gt; 공공 Static Int ExecutenOry ( Sqltraction 트랜잭션 transa, CommandType CMDTYPE, String CMDText, Params Sqlparameter [] CommandParams) {
Sqlcommand CMD = New Sqlcommand () D, Trans.Connection, Trans, CMDTYPE, CMDTEXT, CommandParams); ( cmdType = CommandType.StoredProcedure)
;
val = (int)cmd.Parameters[" @RETURN_VALUE"].Value; val = cmd.ExecuteNonQuery();
} .Clear();
return val;
}
///
/// 返回数据集 DataReader
///
///
/// < param name="cmdText">
///
///
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;
/ / throw new Exception("操작失败!");
throw new Exception(ex.Message);
}
}
///
/// 有事务的取数据
/// < /summary>
///
/// /// & lt; param name = "CommandParams"& gt; & lt;/param & gt;
// & lt; returns & gt;/gt; public static sqldatareader executereader (sqltransational 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;
}
공용 정적 객체 ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] s)
{
try
{
개체 val = cmd.ExecuteScalar();
cmd.Parameters.Clear (); return val;
}
catch
{
throw;
}
드디어
{
conn.Close();
}
}
공용 정적 개체 ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection , 트랜스, cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
/// <요약>
/// 根据Sql语句取得表
///
///
///
///
///
public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable 유혹 가능 = 신규 DataTable();
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, cmdType , cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(temptable);
}
마지막으로
{
conn.Close();
}
반환 유혹;
}
public static DataTable ExecuteTable(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable temptable = new DataTable();
SqlCommand cmd = new S qlCommand();
PrepareCommand(cmd, trans. Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(temptable);
cmd.Parameters.Clear();
유혹적 반환;
}
/// <요약>
/// 根据Sql语句或存储过程取得数据
/// mary>
///
///
///
/// < return>
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlConnection conn = new SqlConnection(ConnectString);
SqlCommand cmd = 새로운 SqlCommand();
DataSet TempDataSet = new DataSet();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlData 어댑터(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameters .Clear();
return TempDataSet;
}
마지막으로
{
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语句或准备
///
///
///
///
///
///
///
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 단위 cm dParms)
cmd.Parameters.Add(parm);
}
}
공용 정적 객체 ToDBValue(객체 값)
{
반환 값 == null ? DBNull.Value : value;
}
public 정적 객체 FromDBValue(객체 dbValue)
{
return dbValue == DBNull.Value ? null : dbValue;
}
}
위 내용은 .net C# SQL 데이터베이스 SQLHelper 클래스 예제 코드 정보의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!