c#如何调用存储过程?

小弟以前在c#里都是调用一条sql语句,没调用过存储过程。现在在学习sql的存储过程,请问编写好后如何在c#内调用呢?求大哥们不吝赐教,小弟先谢过了!... 小弟以前在c#里都是调用一条sql语句,没调用过存储过程。

现在在学习sql的存储过程,请问编写好后如何在c#内调用呢?

求大哥们不吝赐教,小弟先谢过了!
展开
 我来答
xiangjuan314
2016-01-04 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2894万
展开全部
Code highlighting produced 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 RunProcedure(string storedProcName, IDataParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataReader returnReader;
connection.Open();
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
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 RunProcedure(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 RunProcedure(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.StoredProcedure;
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 RunProcedure(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 RunProcedure(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;
}
魔帝疯子
推荐于2018-02-11
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
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.StoredProcedure;//设置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存储过程中的参数。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yenange
2010-01-08 · TA获得超过4537个赞
知道大有可为答主
回答量:2419
采纳率:0%
帮助的人:1637万
展开全部
//加上下面这2句就好了, 不过你记得using System.Data;
string sql="xxx";//存储过程名
CommandType=CommandType.StoreProduce;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
candytree2008
2010-01-08 · TA获得超过313个赞
知道答主
回答量:97
采纳率:0%
帮助的人:141万
展开全部
和其他的SQL语句是一样的 SQL="CALL PROCEDURE_NAME",然后执行SQL就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
flyhigher168
2010-01-08
知道答主
回答量:39
采纳率:0%
帮助的人:18.4万
展开全部
一般我们SqlCommand对像操作的就是Sql语句,这是默认的。但是如果我们指定它的操作类型CommandType为Procedure 然后把Sql语句改为存储过程的名称就OK了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
节盈尾艳蕙
2019-12-03 · TA获得超过3897个赞
知道小有建树答主
回答量:3079
采纳率:27%
帮助的人:458万
展开全部
Code
highlighting
produced
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
RunProcedure(string
storedProcName,
IDataParameter[]
parameters)
{
SqlConnection
connection
=
new
SqlConnection(connectionString);
SqlDataReader
returnReader;
connection.Open();
SqlCommand
command
=
BuildQueryCommand(connection,
storedProcName,
parameters);
command.CommandType
=
CommandType.StoredProcedure;
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
RunProcedure(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
RunProcedure(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.StoredProcedure;
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
RunProcedure(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
RunProcedure(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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式