sqlhelper.cs
来自「1、用SQL查询器打开install目录下的dooogo.sql运行之后创建数据」· CS 代码 · 共 1,188 行 · 第 1/5 页
CS
1,188 行
// ===============================================================================
// Microsoft Data Access Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
//
// SQLHelper.cs
//
// This file contains the implementations of the SqlHelper and SqlHelperParameterCache
// classes.
//
// For more information see the Data Access Application Block Implementation Overview.
// ===============================================================================
// Release history
// VERSION DESCRIPTION
// 2.0 Added support for FillDataset, UpdateDataset and "Param" helper methods
//
// ===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
// ==============================================================================
using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Collections;
namespace Club.Framework.Data
{
/// <summary>
/// The SqlHelper class is intended to encapsulate high performance, scalable best practices for
/// common uses of SqlClient
/// </summary>
public sealed class SqlHelper
{
/// <summary>
/// Calls the SqlCommandBuilder.DeriveParameters, doing any setup and cleanup necessary
/// </summary>
/// <param name="cmd">The SqlCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the SqlCommand. </param>
public static void DeriveParameters( SqlCommand cmd )
{
new SqlServer().DeriveParameters(cmd);
}
#region Private constructor
// Since this class provides only static methods, make the default constructor private to prevent
// instances from being created with "new SqlHelper()"
private SqlHelper() {}
#endregion Private constructor
#region GetParameter
/// <summary>
/// Get a SqlParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="value">The value of the specified parameter</param>
/// <returns>A SqlParameter object</returns>
public static SqlParameter GetParameter( string name, object value )
{
return (SqlParameter)(new SqlServer().GetParameter(name, value));
}
/// <summary>
/// Get a SqlParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="dbType">The System.Data.DbType of the parameter</param>
/// <param name="size">The size of the parameter</param>
/// <param name="direction">The System.Data.ParameterDirection of the parameter</param>
/// <returns>A SqlParameter object</returns>
public static SqlParameter GetParameter ( string name, DbType dbType, int size, ParameterDirection direction )
{
return (SqlParameter)(new SqlServer().GetParameter( name, dbType, size, direction));
}
/// <summary>
/// Get a SqlParameter for use in a SQL command
/// </summary>
/// <param name="name">The name of the parameter to create</param>
/// <param name="dbType">The System.Data.DbType of the parameter</param>
/// <param name="size">The size of the parameter</param>
/// <param name="sourceColumn">The source column of the parameter</param>
/// <param name="sourceVersion">The System.Data.DataRowVersion of the parameter</param>
/// <returns>A SqlParameter object</returns>
public static SqlParameter GetParameter (string name, DbType dbType, int size, string sourceColumn, DataRowVersion sourceVersion )
{
return (SqlParameter)new SqlServer().GetParameter(name, dbType, size, sourceColumn, sourceVersion);
}
#endregion
#region ExecuteNonQuery
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the database specified in
/// the connection string
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(command);
/// </remarks>
/// <param name="command">The SqlCommand to execute</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlCommand command)
{
return new SqlServer().ExecuteNonQuery(command );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in
/// the connection string
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="connectionString">A valid connection string for a SqlConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or T-SQL command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
return new SqlServer().ExecuteNonQuery(connectionString, commandType, commandText );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">A valid connection string for a SqlConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or T-SQL command</param>
/// <param name="commandParameters">An array of SqlParamters used to execute the command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
return new SqlServer().ExecuteNonQuery(connectionString, commandType, commandText, commandParameters);
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in
/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored procedure's return value parameter.
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">A valid connection string for a SqlConnection</param>
/// <param name="spName">The name of the stored prcedure</param>
/// <param name="parameterValues">An array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
return new SqlServer().ExecuteNonQuery(connectionString, spName, parameterValues );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="connection">A valid SqlConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or T-SQL command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
{
return new SqlServer().ExecuteNonQuery(connection, commandType, commandText );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the specified SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">A valid SqlConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or T-SQL command</param>
/// <param name="commandParameters">An array of SqlParamters used to execute the command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
return new SqlServer().ExecuteNonQuery(connection, commandType, commandText, commandParameters );
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlConnection
/// using the provided parameter values. This method will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored procedure's return value parameter.
///
/// e.g.:
/// int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connection">A valid SqlConnection</param>
/// <param name="spName">The name of the stored procedure</param>
/// <param name="parameterValues">An array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
{
return new SqlServer().ExecuteNonQuery(connection, spName, parameterValues );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlTransaction.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="transaction">A valid SqlTransaction</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or T-SQL command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
{
return new SqlServer().ExecuteNonQuery(transaction, commandType, commandText );
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction
/// using the provided parameters.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?