如何使用C#在asp.net中使用存储过程插入值 [英] how to insert the value using store procedure in asp.net using C#

查看:61
本文介绍了如何使用C#在asp.net中使用存储过程插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的跑步模式如下



教师(标签容器)



当我点击标签时gridview中的容器如下



每个下拉列表评级从差到优等



1.Poor,2 .Fair,3.Good,4.非常好,5.优秀。当用户选择要保存在数据库中的Good means 3值时,在dropwonlist下面。 (对于drodpownlist1到dropdownlist10)



教师姓名问题1问题2问题3问题4问题5



XXX dropdownlist1 dropdownlist2 dropdownlist3 dropdownlist4 dropdownlist5

YYY dropdownlist6 dropdownlist7 dropdownlist8 dropdownlist9 dropdownlist10



我在esubmit上如下



我想在表格中保存上面的gridview记录。



教师表结构如下



Facid Autoincrment

Facname varchar(50)

Question1 varchar(50)

Question2 varchar(50)

Question3 varchar(50)

Question4 varchar(50)

Question5 varchar(50)





在表中我想显示如下记录



Facid Facname Questi on1 Question2 Question3 Question4 Question5

1 XXX 5 4 3 2 1

2 YYY 5 4 3 2 1





i希望在该商店程序中创建商店程序我想使用商店程序在Faculty表中插入记录。





存储过程如下



My run mode as follows

Faculty(Tab Container)

When i click the tab container in gridview as follows

For each dropdownlist rating from Poor to excellent

1.Poor,2.Fair,3.Good,4.Very Good,5.Excellent. in teh below dropwonlist when user select the Good means 3 value to be saved in the database. (for drodpownlist1 to dropdownlist10)

Faculty Name Question1 Question2 Question3 Question4 Question5

XXX dropdownlist1 dropdownlist2 dropdownlist3 dropdownlist4 dropdownlist5
YYY dropdownlist6 dropdownlist7 dropdownlist8 dropdownlist9 dropdownlist10

I have on esubmit as follows

I want to save the above gridview records in table.

Faculty table Structure as follows

Facid Autoincrment
Facname varchar(50)
Question1 varchar(50)
Question2 varchar(50)
Question3 varchar(50)
Question4 varchar(50)
Question5 varchar(50)


In table i want to shows the record as follows

Facid Facname Question1 Question2 Question3 Question4 Question5
1 XXX 5 4 3 2 1
2 YYY 5 4 3 2 1


i want to create store procedure in that store procedure i want to insert the record in the Faculty table using store procedure.


Store procedure as follows

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


 ALTER procedure [dbo].[PreseaFac]
 as 
 begin

  declare @Facid  varchar(50),
          @Facname varchar(50),
          @FacQ1 varchar(50),
          @FacQ2 varchar(50),
          @FacQ3 varchar(50),
          @FacQ4 varchar(50),
          @FacQ5 varchar(50)

  insert into Tb_Faculty_Feedback (Facid ,Facname,FacQ1,FacQ2,FacQ3,FacQ4,FacQ5) 
  values (@Fid,@Facid,@FacQ1,@FacQ2,@FacQ3,@FacQ4,@FacQ5)
  end.





在上面的商店程序中如何插入评级差表示1值使用商店程序保存在表格中。



我在该子状按钮中有一个提交按钮,我称之为商店程序。





为什么我可以使用商店程序插入价值。



问候,

Jegan B.



in the above store procedure how to insert the ratings poor means 1 value to saved in the table using store procedure.

I have one submit button in that subit button i call the store procedure.


for that how can i insert the value using store procedure.

Regards,
Jegan B.

推荐答案

您好朋友,根据我对此问题的理解,您必须进行以下更改:



首先使存储过程参数化,意味着你必须添加参数,以便您可以传递代码中的值:



Hello friend, as per my understanding of this problem, you have to do the following changes:

First of all make the stored procedure parametric, means you have to add parameters so that you can pass the values from your code behind:

ALTER procedure [dbo].[PreseaFac]
	@Facid  varchar(50),
	@Facname varchar(50),
	@FacQ1 varchar(50),
	@FacQ2 varchar(50),
        @FacQ3 varchar(50),
        @FacQ4 varchar(50),
        @FacQ5 varchar(50)
AS
BEGIN
	INSERT INTO Tb_Faculty_Feedback 
	(Facid ,Facname, FacQ1, FacQ2, FacQ3, FacQ4, FacQ5) 
	VALUES (@Fid,@Facid,@FacQ1,@FacQ2,@FacQ3,@FacQ4,@FacQ5)
END



我假设您必须在aspx页面上添加控件,如下所示:


I assume you must have added controls on your aspx page as follows:

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="1">Poor</asp:ListItem>
    <asp:ListItem Value="2">Fair</asp:ListItem>
    <asp:ListItem Value="3">Good</asp:ListItem>
    <asp:ListItem Value="4">Very Good</asp:ListItem>
    <asp:ListItem Value="5">Excellent</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Value="1">Poor</asp:ListItem>
    <asp:ListItem Value="2">Fair</asp:ListItem>
    <asp:ListItem Value="3">Good</asp:ListItem>
    <asp:ListItem Value="4">Very Good</asp:ListItem>
    <asp:ListItem Value="5">Excellent</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
    <asp:ListItem Value="1">Poor</asp:ListItem>
    <asp:ListItem Value="2">Fair</asp:ListItem>
    <asp:ListItem Value="3">Good</asp:ListItem>
    <asp:ListItem Value="4">Very Good</asp:ListItem>
    <asp:ListItem Value="5">Excellent</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server">
    <asp:ListItem Value="1">Poor</asp:ListItem>
    <asp:ListItem Value="2">Fair</asp:ListItem>
    <asp:ListItem Value="3">Good</asp:ListItem>
    <asp:ListItem Value="4">Very Good</asp:ListItem>
    <asp:ListItem Value="5">Excellent</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList5" runat="server">
    <asp:ListItem Value="1">Poor</asp:ListItem>
    <asp:ListItem Value="2">Fair</asp:ListItem>
    <asp:ListItem Value="3">Good</asp:ListItem>
    <asp:ListItem Value="4">Very Good</asp:ListItem>
    <asp:ListItem Value="5">Excellent</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />







然后你必须编写ADO.NET代码来从Submit按钮调用这个存储过程点击如下:




Then you have to write ADO.NET code to call this stored procedure from the Submit button click as follows:

string conStr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
    con.Open();

    SqlParameter Facid = new SqlParameter();
    Facid.ParameterName = "@Facid";
    Facid.Value = "1";  // passing a hard coded value

    SqlParameter Facname = new SqlParameter();
    Facname.ParameterName = "@Facname";
    Facname.Value = "DD"; // passing a hard coded value

    SqlParameter FacQ1 = new SqlParameter();
    FacQ1.ParameterName = "@FacQ1";
    FacQ1.Value = DropDownList1.SelectedValue;

    SqlParameter FacQ2 = new SqlParameter();
    FacQ2.ParameterName = "@FacQ2";
    FacQ2.Value = DropDownList2.SelectedValue;

    SqlParameter FacQ3 = new SqlParameter();
    FacQ3.ParameterName = "@FacQ3";
    FacQ3.Value = DropDownList3.SelectedValue;

    SqlParameter FacQ4 = new SqlParameter();
    FacQ4.ParameterName = "@FacQ4";
    FacQ4.Value = DropDownList4.SelectedValue;

    SqlParameter FacQ5 = new SqlParameter();
    FacQ5.ParameterName = "@FacQ5";
    FacQ5.Value = DropDownList5.SelectedValue;

    SqlCommand cmd = new SqlCommand("PreseaFac", con);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(Facid);
    cmd.Parameters.Add(Facname);
    cmd.Parameters.Add(FacQ1);
    cmd.Parameters.Add(FacQ2);
    cmd.Parameters.Add(FacQ3);
    cmd.Parameters.Add(FacQ4);
    cmd.Parameters.Add(FacQ5);

    cmd.ExecuteNonQuery();
}



注意:我保持数据库更改不变。请确保您也实施了一些异常处理。



- DD


Note: I have kept your database changes intact. Please make sure that you implement some exception handling as well.

- DD


这篇关于如何使用C#在asp.net中使用存储过程插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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