mit System;
mit System.Collections.Generic;
mit System.Text;
mit System.Collections;
mit System.Data.SqlClient;
mit System.Data;
using System.Configuration;
öffentliche Klasse SQLHelper
{
//Rufen Sie die Datenbankverbindungskonfiguration in web.config ab
öffentliche statische schreibgeschützte Zeichenfolge ConnectString = ConfigurationManager.ConnectionStrings [ "DBString"].ConnectionString;
///
param name="cmdType">Gespeicherte Prozedur oder SQL-Anweisung
// & lt; name = "CommandParams" & gt; Parameterliste & lt;/Param & GT;
//// & lt;
PrepareCommand( cmd, conn, null, cmdType, cmdText, CommandParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val. ;
; .Close() Name der gespeicherten Prozedur oder SQL-Anweisung
;/retouren>
PrepareCommand( cmd, trans.Connection, trans, cmdType , cmdText, CommandParams); ", "") .Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
val = (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
. else
if (cmdType==CommandType.Text)
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear ();
return val;
}
///
/// 返回数据集 DataReader
///
/// param>
///
///
///
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new d();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataReader. rd r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
// neue Ausnahme auslösen ("操作失败!");
throw new Exception(ex.Message);
有事务的取数据
///
///
///
///
///
public static SqlDataReader ExecuteReader(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear ();
zurück rdr;
}
öffentliches statisches Objekt ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
SqlCommand cmd = new qlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
cmd.Parameters.Clear();
return val; {
throw;
}
endlich
{
conn.Close();
}
}
öffentliches statisches Objekt 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;
}
/// & lt; summary & gt;
/// 根据 sql 语句 表 表
/// & lt;/summary & gt;
/// & lt; param name = "cmdtype" & gt "& gt ;
///
///
///
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); }
public static DataTable ExecuteTable (SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(t leer);
cmd.Parameters.Clear();
}
///
///
//////
/// ///
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
{
SqlDataAdapter. sql Adapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);cmd.Parameters.Clear();
return TempDataSet;
}
final ly
{
conn.Close() ;
}
public static DataSet ExecuteDataSet(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
DataSet TempDataSet = neues DataSet( );
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameter .Clear();
return TempDataSet;
//
///
/// < param name="trans">
///
///
///
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter [] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd .CommandText = cmdText;
cmd.Transaction = trans;
if (cmdParms != null)
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm); }
öffentliches statisches Objekt ToDBValue(Objektwert)
{
Rückgabewert == null ? DBNull.Value : value;
}
public static object FromDBValue(object dbValue)
{
return dbValue == DBNull.Value ? null : dbValue;
}
Das obige ist der detaillierte Inhalt vonInformationen zum Beispielcode der SQLHelper-Klasse für die .net C#-SQL-Datenbank. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!