c#如何调用存储过程?
发布网友
发布时间:2022-06-01 20:49
我来回答
共6个回答
热心网友
时间:2023-10-16 06:07
Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
/// <summary>
/// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader RunProcere(string storedProcName, IDataParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataReader returnReader;
connection.Open();
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcere;
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="tableName">DataSet结果中的表名</param>
/// <returns>DataSet</returns>
public static DataSet RunProcere(string storedProcName, IDataParameter[] parameters, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
public static DataSet RunProcere(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.SelectCommand.CommandTimeout = Times;
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
/// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcere;
foreach (SqlParameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以DBNull.Value.
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
return command;
}
/// <summary>
/// 执行存储过程,返回Output输出参数值
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>object</returns>
public static object RunProcere(string storedProcName, IDataParameter[] paramenters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = BuildQueryCommand(connection, storedProcName, paramenters);
command.ExecuteNonQuery();
object obj=command.Parameters["@Output_Value"].Value; //@Output_Value和具体的存储过程参数对应
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
}
/// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="rowsAffected">影响的行数</param>
/// <returns></returns>
public static int RunProcere(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}
/// <summary>
/// 创建 SqlCommand 对象实例(用来返回一个整数值)
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand 对象实例</returns>
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
热心网友
时间:2023-10-16 06:07
string connstring = @"Data Source=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcere;//设置cmd的类型为存储过程
cmd.CommandText = "CustOrderHist";
cmd.Connection = conn;
SqlParameter pCustomerID = new SqlParameter();
pCustomerID.ParameterName = "@CustomerID";
pCustomerID.SqlDbType = SqlDbType.NChar;
pCustomerID.Value = "ALFKI";
cmd.Parameters.Add(pCustomerID);
CustOrderHist:存储过程名称,@CustomerID存储过程中的参数。
热心网友
时间:2023-10-16 06:08
//加上下面这2句就好了, 不过你记得using System.Data;
string sql="xxx";//存储过程名
CommandType=CommandType.StoreProce;
热心网友
时间:2023-10-16 06:08
和其他的SQL语句是一样的 SQL="CALL PROCEDURE_NAME",然后执行SQL就行了
热心网友
时间:2023-10-16 06:09
一般我们SqlCommand对像操作的就是Sql语句,这是默认的。但是如果我们指定它的操作类型CommandType为Procere 然后把Sql语句改为存储过程的名称就OK了
热心网友
时间:2023-10-16 06:10
Code
highlighting
proced
by
Actipro
CodeHighlighter
(freeware)http://www.CodeHighlighter.com/-->/数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
public
static
string
connectionString
=
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
///
<summary>
///
执行存储过程,返回SqlDataReader
(
注意:调用该方法后,一定要对SqlDataReader进行Close
)
///
</summary>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<returns>SqlDataReader</returns>
public
static
SqlDataReader
RunProcere(string
storedProcName,
IDataParameter[]
parameters)
{
SqlConnection
connection
=
new
SqlConnection(connectionString);
SqlDataReader
returnReader;
connection.Open();
SqlCommand
command
=
BuildQueryCommand(connection,
storedProcName,
parameters);
command.CommandType
=
CommandType.StoredProcere;
returnReader
=
command.ExecuteReader(CommandBehavior.CloseConnection);
return
returnReader;
}
///
<summary>
///
执行存储过程
///
</summary>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<param
name="tableName">DataSet结果中的表名</param>
///
<returns>DataSet</returns>
public
static
DataSet
RunProcere(string
storedProcName,
IDataParameter[]
parameters,
string
tableName)
{
using
(SqlConnection
connection
=
new
SqlConnection(connectionString))
{
DataSet
dataSet
=
new
DataSet();
connection.Open();
SqlDataAdapter
sqlDA
=
new
SqlDataAdapter();
sqlDA.SelectCommand
=
BuildQueryCommand(connection,
storedProcName,
parameters);
sqlDA.Fill(dataSet,
tableName);
connection.Close();
return
dataSet;
}
}
public
static
DataSet
RunProcere(string
storedProcName,
IDataParameter[]
parameters,
string
tableName,
int
Times)
{
using
(SqlConnection
connection
=
new
SqlConnection(connectionString))
{
DataSet
dataSet
=
new
DataSet();
connection.Open();
SqlDataAdapter
sqlDA
=
new
SqlDataAdapter();
sqlDA.SelectCommand
=
BuildQueryCommand(connection,
storedProcName,
parameters);
sqlDA.SelectCommand.CommandTimeout
=
Times;
sqlDA.Fill(dataSet,
tableName);
connection.Close();
return
dataSet;
}
}
///
<summary>
///
构建
SqlCommand
对象(用来返回一个结果集,而不是一个整数值)
///
</summary>
///
<param
name="connection">数据库连接</param>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<returns>SqlCommand</returns>
private
static
SqlCommand
BuildQueryCommand(SqlConnection
connection,
string
storedProcName,
IDataParameter[]
parameters)
{
SqlCommand
command
=
new
SqlCommand(storedProcName,
connection);
command.CommandType
=
CommandType.StoredProcere;
foreach
(SqlParameter
parameter
in
parameters)
{
if
(parameter
!=
null)
{
//
检查未分配值的输出参数,将其分配以DBNull.Value.
if
((parameter.Direction
==
ParameterDirection.InputOutput
||
parameter.Direction
==
ParameterDirection.Input)
&&
(parameter.Value
==
null))
{
parameter.Value
=
DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
return
command;
}
///
<summary>
///
执行存储过程,返回Output输出参数值
///
</summary>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<returns>object</returns>
public
static
object
RunProcere(string
storedProcName,
IDataParameter[]
paramenters)
{
using
(SqlConnection
connection
=
new
SqlConnection(connectionString))
{
connection.Open();
SqlCommand
command
=
BuildQueryCommand(connection,
storedProcName,
paramenters);
command.ExecuteNonQuery();
object
obj=command.Parameters["@Output_Value"].Value;
//@Output_Value和具体的存储过程参数对应
if
((Object.Equals(obj,
null))
||
(Object.Equals(obj,
System.DBNull.Value)))
{
return
null;
}
else
{
return
obj;
}
}
}
///
<summary>
///
执行存储过程,返回影响的行数
///
</summary>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<param
name="rowsAffected">影响的行数</param>
///
<returns></returns>
public
static
int
RunProcere(string
storedProcName,
IDataParameter[]
parameters,
out
int
rowsAffected)
{
using
(SqlConnection
connection
=
new
SqlConnection(connectionString))
{
int
result;
connection.Open();
SqlCommand
command
=
BuildIntCommand(connection,
storedProcName,
parameters);
rowsAffected
=
command.ExecuteNonQuery();
result
=
(int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return
result;
}
}
///
<summary>
///
创建
SqlCommand
对象实例(用来返回一个整数值)
///
</summary>
///
<param
name="storedProcName">存储过程名</param>
///
<param
name="parameters">存储过程参数</param>
///
<returns>SqlCommand
对象实例</returns>
private
static
SqlCommand
BuildIntCommand(SqlConnection
connection,
string
storedProcName,
IDataParameter[]
parameters)
{
SqlCommand
command
=
BuildQueryCommand(connection,
storedProcName,
parameters);
command.Parameters.Add(new
SqlParameter("ReturnValue",
SqlDbType.Int,
4,
ParameterDirection.ReturnValue,
false,
0,
0,
string.Empty,
DataRowVersion.Default,
null));
return
command;
}