ADO.Net Connectivity using Stored Procedure and Output parameters
来源:互联网 发布:中国城市化进程数据 编辑:程序博客网 时间:2024/06/08 09:08
原文:http://www.codeproject.com/Tips/990005/ADO-Net-Connectivity-using-Stored-Procedure-and-Ou
Create Database OutputParameterDemoUse OutputParameterDemoCreate Table Employee(Emp_ID int identity primary key, Name nvarchar(40), Designation nvarchar(100), Salary int)Select * from EmployeeInsert into Employee values ('Ankit','Software Engineer',45000)Insert into Employee values ('Max','Software Engineer',55000)Insert into Employee values ('Mac','Software Engineer',9000)Insert into Employee values ('Jane','Software Engineer',21000)Insert into Employee values ('Joseph','Software Engineer',10000)Insert into Employee values ('Jason','Software Engineer',32000)Insert into Employee values ('Sumit','Software Engineer',85000)Create Procedure spOutputParameter@Name nvarchar(40), @Designation nvarchar(100), @Salary int, @Emp_ID int outasBegin Insert into Employee Values (@Name, @Designation, @Salary) Select @Emp_ID = SCOPE_IDENTITY()End
string CS = ConfigurationManager.ConnectionStrings["DatabaseCS"].ConnectionString; protected void btnSubmit_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(CS)) { SqlCommand cmd = new SqlCommand("spOutputParameter", con); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", txtName.Text); cmd.Parameters.AddWithValue("@Designation", ddlDesignation.SelectedValue); cmd.Parameters.AddWithValue("@Salary", txtSalary.Text); SqlParameter outputPara = new SqlParameter(); outputPara.ParameterName = "@Emp_ID"; outputPara.Direction = System.Data.ParameterDirection.Output; outputPara.SqlDbType = System.Data.SqlDbType.Int; cmd.Parameters.Add(outputPara); con.Open(); cmd.ExecuteNonQuery(); string RetrievedEmpId = outputPara.Value.ToString(); lblStatus.Text = "Your Employee Id is : " + RetrievedEmpId; } }
other test demo:
ALTER PROCEDURE [dbo].[BuilderOrderID] @NEWORDERID bigint outputASBEGININSERT INTO [OrderIder] ([CreateTime])VALUES (getdate())Select @NEWORDERID=@@IDENTITYEND
//Microsoft.Practices.EnterpriseLibrary.Data.dll, v2.0.50727//Microsoft.Practices.EnterpriseLibrary.Data
protected static Database db; public DbBase() { if (db == null) db = DatabaseFactory.CreateDatabase("dbConnectionString"); }
public class OrderIderContext : DbBase { public bool Builder(out int newOrderId) { newOrderId = 0; DbCommand dbCommandWrapper = db.GetStoredProcCommand("[BuilderOrderID]"); db.AddOutParameter(dbCommandWrapper, "@NEWORDERID", DbType.Int32,32); int result = db.ExecuteNonQuery(dbCommandWrapper); if (result > 0) { newOrderId = Convert.ToInt32(db.GetParameterValue(dbCommandWrapper, "@NEWORDERID").ToString()); } return result > 0; } }
0 0
- ADO.Net Connectivity using Stored Procedure and Output parameters
- Csharp: Calling Output Parameters and Return Values in ADO.NET
- Csharp: Calling Output Parameters and Return Values in ADO.NET
- MySQL Stored Procedure Parameters
- csharp: Oracle Stored Procedure DAL using ODP.NET
- csharp: Oracle Stored Procedure DAL using ODP.NET
- ADO.NET 如何取得 Stored Procedure 的回傳值 (如何获得Stored Procedure 的Last inset id?)
- Accessing Oracle 9i Stored Procedures Using ADO.NET
- Using DataAdapters and stored procedures with Sybase and ODBC.NET
- Building DB2 Stored Procedure Using WSAD V5
- MySQL Stored Procedure + C# Using ODBC
- csharp: MySQL Stored Procedure using DAL
- How to call an Oracle Stored Procedure that returns one or more REF CURSORS, using ADO from C++ How
- ado.net中的parameters
- Differences between Trigger and Stored Procedure
- ORDER, SUM and Stored Procedure in MySQL
- Why Stored Procedure and Why not...
- One Importance Difference between Stored Procedure and Stored Function
- HDU 4054 Hexadecimal View(模拟)
- HDU 4055 Number String(dp)
- HDU 4006 The kth great number(优先队列)
- git review (四)
- static_cast与dynamic_cast转换
- ADO.Net Connectivity using Stored Procedure and Output parameters
- erlang虚拟机代码执行原理
- php单一入口模式详细讲解
- C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助。 Const 是C++中常用的类型修饰符,常类型是指使用类
- c++一些转换操作
- Soap 协议 详解
- LA 4945 Free Goodies(贪心)
- 我写的博客呢???
- 华为校园实习生招聘总结与感悟