我需要在数据库上执行存储过程。这是我到目前为止所得到的有效方法:
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 会做到这一点,但还没有发现任何有效的方法。假设必须执行名为“initialize”的存储过程。我怎么做?
对于 SqlServer,这可能是这样的:
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)