执行使用的DbConnection存储过程 [英] Executing a stored procedure using a DbConnection

查看:478
本文介绍了执行使用的DbConnection存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在数据库中执行存储过程。这是我到目前为止的作品:

I need to execute a stored procedure on a database. This is what I got so far that works:

protected DbProviderFactory dbProviderFactory;
this.dbProviderFactory = DalFactory.GetFactory(this.adapterConfiguration);

DbConnection dbConnection = dbProviderFactory.CreateConnection();

dbConnection.ConnectionString = this.adapterConfiguration.DatabaseInformation.ExternalDatabaseInformation.connectionString;
            try
            {
                dbConnection.Open();
            }
            catch (Exception e)
            {

                throw;
            }

我怀疑的DbCommand将做到这一点,但还没有找到任何工作。比方说,由名字的存储过程初始化必须执行。我该怎么办呢?

I suspect that DbCommand would do it, but haven't found anything working. Let's say that the stored procedure by the name "initialize" has to be executed. How do I do that?

推荐答案

有关SQLSERVER,这可能是这样的:

For SqlServer, this could be like this :

DbCommand command = new SqlCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "<your stored proc>";
command.Connection = dbConnection;

SqlParameter param1 = new SqlParameter("<your parameter>", MyVar);
command.Parameters.Add(param1);
//[...]

SqlParameter returnValue = new SqlParameter("ReturnValue", User);
returnValue.Direction = System.Data.ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);

command.Connection.Open();
command.ExecuteNonQuery();
int result = (int)command.Parameters["ReturnValue"].Value;
command.Connection.Close();

这篇关于执行使用的DbConnection存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆