.Net里怎么得到存储过程的返回值

前端之家收集整理的这篇文章主要介绍了.Net里怎么得到存储过程的返回值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

除了输入和输出参数之外,存储过程还可以具有返回值。以下示例阐释   ADO.NET   如何发送和接收输入参数、输出参数和返回值,其中采用了这样一种常见方案:将新记录插入其中主键列是自动编号字段的表。该示例使用输出参数来返回自动编号字段的   @@Identity,而   DataAdapter   则将其绑定到   DataTable   的列,使   DataSet   反映所生成的主键值。     该示例使用以下存储过程将新目录插入   Northwind   Categories   表(该表将   CategoryName   列中的值当作输入参数),从   @@Identity   中以输出参数的形式返回自动编号字段   CategoryID   的值,并提供所影响行数的返回值。     CREATE   PROCEDURE   InsertCategory         @CategoryName   nchar(15),        @Identity   int   OUT     AS     INSERT   INTO   Categories   (CategoryName)   VALUES(@CategoryName)     SET   @Identity   =   @@Identity     RETURN   @@ROWCOUNT     以下示例将   InsertCategory   存储过程用作   DataAdapter   的   InsertCommand   的数据源。通过将   CategoryID   列指定为   @Identity   输出参数的   SourceColumn,当调用   DataAdapter   的   Update   方法时,所生成自动编号值将在该记录插入数据库后在   DataSet   中得到反映。     对于   OleDbDataAdapter,必须在指定其他参数之前先指定   ParameterDirection   为   ReturnValue   的参数。     sqlClient     [Visual   Basic]     Dim   nwindConn   As   sqlConnection   =   New   sqlConnection("Data   Source=localhost;Integrated   Security=SSPI;"   &   _                                                                                                                 "Initial   Catalog=northwind")         Dim   catDA   As   sqlDataAdapter   =   New   sqlDataAdapter("SELECT   CategoryID,  CategoryName   FROM   Categories",  nwindConn)         catDA.InsertCommand   =   New   sqlCommand("InsertCategory"  ,  nwindConn)     catDA.InsertCommand.CommandType   =   CommandType.StoredProcedure         Dim   myParm   As   sqlParameter   =   catDA.InsertCommand.Parameters.Add("@RowCount",  sqlDbType.Int)     myParm.Direction   =   ParameterDirection.ReturnValue         catDA.InsertCommand.Parameters.Add("@CategoryName",  sqlDbType.NChar,  15,  "CategoryName")         myParm   =   catDA.InsertCommand.Parameters.Add("@Identity",  sqlDbType.Int,  0,  "CategoryID")     myParm.Direction   =   ParameterDirection.Output         Dim   catDS   As   DataSet   =   New   DataSet()     catDA.Fill(catDS,  "Categories")         Dim   newRow   As   DataRow   =   catDS.Tables("Categories").NewRow()     newRow("CategoryName")   =   "New   Category"     catDS.Tables("Categories").Rows.Add(newRow)         catDA.Update(catDS,  "Categories")         Dim   rowCount   As   Int32   =   CInt(catDA.InsertCommand.Parameters("@RowCount").Value)     [C#]     sqlConnection   nwindConn   =   new   sqlConnection("Data   Source=localhost;Integrated   Security=SSPI;"   +                                                                                             "Initial   Catalog=northwind");         sqlDataAdapter   catDA   =   new   sqlDataAdapter("SELECT   CategoryID,  nwindConn);         catDA.InsertCommand   =   new   sqlCommand("InsertCategory",  nwindConn);     catDA.InsertCommand.CommandType   =   CommandType.StoredProcedure;         sqlParameter   myParm   =   catDA.InsertCommand.Parameters.Add("@RowCount",  sqlDbType.Int);     myParm.Direction   =   ParameterDirection.ReturnValue;         catDA.InsertCommand.Parameters.Add("@CategoryName",  "CategoryName");         myParm   =   catDA.InsertCommand.Parameters.Add("@Identity",  "CategoryID");     myParm.Direction   =   ParameterDirection.Output;         DataSet   catDS   =   new   DataSet();     catDA.Fill(catDS,  "Categories");         DataRow   newRow   =   catDS.Tables["Categories"].NewRow();     newRow["CategoryName"]   =   "New   Category";     catDS.Tables["Categories"].Rows.Add(newRow);         catDA.Update(catDS,  "Categories");         Int32   rowCount   =   (Int32)catDA.InsertCommand.Parameters["@RowCount"].Value;     OleDb     [Visual   Basic]     Dim   nwindConn         As   OleDbConnection   =   New   OleDbConnection("Provider=sqlOLEDB;Data   Source=localhost;"   &   _                                                                                                                         "Integrated   Security=SSPI;Initial   Catalog=northwind")         Dim   catDA   As   OleDbDataAdapter   =   New   OleDbDataAdapter("SELECT   CategoryID,  _                                                                                                               nwindConn)         catDA.InsertCommand   =   New   OleDbCommand("InsertCategory"  ,  nwindConn)     catDA.InsertCommand.CommandType   =   CommandType.StoredProcedure         Dim   myParm   As   OleDbParameter   =   catDA.InsertCommand.Parameters.Add("@RowCount",  OleDbType.Integer)     myParm.Direction   =   ParameterDirection.ReturnValue         catDA.InsertCommand.Parameters.Add("@CategoryName",  OleDbType.Char,  OleDbType.Integer,  "Categories")         Dim   rowCount   As   Int32   =   CInt(catDA.InsertCommand.Parameters("@RowCount").Value)     [C#]     OleDbConnection   nwindConn   =   new   OleDbConnection("Provider=sqlOLEDB;Data   Source=localhost;"   +                                                                                                       "Integrated   Security=SSPI;Initial   Catalog=northwind");         OleDbDataAdapter   catDA   =   new   OleDbDataAdapter("SELECT   CategoryID,  nwindConn);         catDA.InsertCommand   =   new   OleDbCommand("InsertCategory",  nwindConn);     catDA.InsertCommand.CommandType   =   CommandType.StoredProcedure;         OleDbParameter   myParm   =   catDA.InsertCommand.Parameters.Add("@RowCount",  OleDbType.Integer);     myParm.Direction   =   ParameterDirection.ReturnValue;         catDA.InsertCommand.Parameters.Add("@CategoryName",  "Categories");         Int32   rowCount   =   (Int32)catDA.InsertCommand.Parameters["@RowCount"].Value;

.NET中如何调用存储过程         http://www.5d.cn/Tutorial/webdevelop/asp/200412/1960.html

原文链接:https://www.f2er.com/csharp/74606.html

猜你在找的C#相关文章