ADOSQL Server 2014Windows 7.NET4.5Visual Studio 2013ADO.NETASP.NET4.NET 3.5C# 5.0C# 3.5HTMLIntermediateVisual StudioSQL ServerSQLWindows.NETASP.NETC#
使用存储过程和输出参数的 ADO.Net 连接





1.00/5 (2投票s)
本文介绍了如何使用带有输出参数的存储过程将数据插入 SQL 数据库。 还使用了一些引导程序来创建一个美观的 HTML。
引言
使用输出参数将数据插入 SQL 表,然后在 ASP.NET Webform 上检索自动生成的 ID。
背景
ADO.NET, SQL, HTML, C#
SQL 代码
Create Database OutputParameterDemo Use OutputParameterDemo Create Table Employee(Emp_ID int identity primary key, Name nvarchar(40), Designation nvarchar(100), Salary int) Select * from Employee Insert 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 out as Begin Insert into Employee Values (@Name, @Designation, @Salary) Select @Emp_ID = SCOPE_IDENTITY() End
Visual Studio 中的 Web 应用程序
WebForm1.aspx
<!DOCTYPE html> <html> <head runat="server"> <title>Output parameters Demo</title> <link href="Content/bootstrap.min.css" rel="stylesheet" /> <meta name="description" content="Output parameters Demo" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <form id="form1" runat="server"> <div class="jumbotron"> <h1 class="text-capitalize text-center">SP Output parameter Demo</h1> </div> <div class="container"> <table class="table table-hover"> <tr> <td> <asp:Label ID="lblName" runat="server" Text="Name" CssClass="label label-default" Font-Size="Large"></asp:Label></td> <td> <asp:TextBox ID="txtName" runat="server" CssClass="input-sm"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lblDesignation" runat="server" Text="Designation" CssClass="label label-default" Font-Size="Large"></asp:Label></td> <td> <asp:DropDownList ID="ddlDesignation" runat="server"> <asp:ListItem>Software Engineer</asp:ListItem> <asp:ListItem>Senior Software Engineer</asp:ListItem> <asp:ListItem>Contract Trainee</asp:ListItem> <asp:ListItem>Project Lead</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td> <asp:Label ID="lblSalary" runat="server" Text="Salary" CssClass="label label-default" Font-Size="Large"></asp:Label></td> <td> <asp:TextBox ID="txtSalary" runat="server" CssClass="input-sm"></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnSubmit" runat="server" Text="Save Data" CssClass="btn btn-danger btn-lg" OnClick="btnSubmit_Click" /> </td> </tr> <tr> <td> <asp:Label ID="lblStatus" runat="server" CssClass="label label-danger text-info" Font-Size="Large"></asp:Label></td> </tr> </table> </div> </form> </body> </html>
Webform1.aspx.cs
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; } }
Web.config
<configuration> <connectionStrings> <add name="DatabaseCS" providerName="System.Data.SqlClient" connectionString="data source = .; Initial Catalog = OutputParameterDemo; Integrated Security = true"/> </connectionStrings> </configuration>
SQL 查询说明
- 创建了一个数据库和一个表,该表将 Emp_Id 列作为主键,并且是一个标识列,即它以整数形式生成 Id,并且每次插入行时将其递增 1。
- 插入了一些示例数据。
- 创建了存储过程,该过程需要 4 个参数,其中一个是输出参数 @Emp_Id。
- 在 Begin 和 End 范围之间,我们使用参数将数据插入到表中,然后使用 Select 语句,我们使用 SCOPE_IDENTITY() 函数获取插入的员工的 Id,该函数返回表中最后插入的行。
Webform1.aspx 说明
- 使用了简单的 HTML。 仅仅使用了 4 个标签、2 个文本框、1 个下拉列表和一个插入数据的按钮。
- 使用引导程序使表单看起来漂亮。 我将发布一篇包含 Bootstrap3 完整演练的文章。
Web.config 说明
我们在 Web.config 文件中包含了我们的连接字符串,这样我们就不必为每个 webform 反复编写连接字符串。
- Name 属性用于向连接字符串添加有意义的名称。
- providerName 包含将支持该功能的命名空间。
- connectionString 是用于连接数据库的字符串。 (.) 表示我们正在连接到本地数据库。 Initial Catalog 是数据库的名称。
Webform1.aspx.cs 说明
- 创建了 SqlConnection 并在 using 语句中使用它,以便在工作完成后自动关闭连接。
- 创建了 SqlCommand,并将存储过程的名称作为参数传递。
- 参数被添加到命令对象中,其名称与我们在 SQL 中创建的存储过程的参数名称相同,并且指定了应该从哪个控件获取每个参数的值。
- 为输出参数创建了 SqlParameter,并使用 SqlParameter 类的各个属性指定了参数的名称、方向和数据类型。 该对象作为参数添加到命令对象。
- 打开连接并使用 ExecuteNonQuery() 插入数据。
- 输出参数值转换为字符串,因为我们希望以字符串的形式显示 Id。
- 输出参数的值显示在标签上,这有助于我们完成我们的目标。
希望您觉得这篇文章有用。 我很快会发布更多文章..
如有任何疑问,请发表评论。