关于asp.net2.0中存储过程的调用

     因为我是新手,所以以前写东西都没用过调用存储过程,昨天在用这个的时候突然觉得自己定义一个调用太麻烦不太符合2.0的宗旨 而且总会报一些 无法转换类型的莫名其妙的错误.

      研究了一下MSDN发现,还真有简单方法 现在大家就来看下msdn的例子

<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Script runat="server">
private void InsertShipper (object source, EventArgs e) {
  SqlDataSource1.Insert();
}
</Script>
<HTML>
  <BODY>
    <FORM runat="server">

      <asp:dropdownlist
        id="DropDownList1"
        runat="server"
        datasourceid="SqlDataSource1"
        datatextfield="CompanyName"
        datavaluefield="ShipperID" />

<!-- Security Note: The SqlDataSource uses a FormParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the FormParameter, handle the Inserting event. -->

      <asp:sqldatasource
        id="SqlDataSource1"
        runat="server"
        connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
        selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
        insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
          <insertparameters>
            <asp:formparameter name="CoName" formfield="CompanyNameBox" />
            <asp:formparameter name="Phone"  formfield="PhoneBox" />

          </insertparameters>
      </asp:sqldatasource>

      <p><asp:textbox
           id="CompanyNameBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator1"
        runat="server"
        ControlToValidate="CompanyNameBox"
        Display="Static"
        ErrorMessage="Please enter a company name." />

      <p><asp:textbox
           id="PhoneBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator2"
        runat="server"
        ControlToValidate="PhoneBox"
        Display="Static"
        ErrorMessage="Please enter a phone number." />

      <p><asp:button
           id="Button1"
           runat="server"
           text="Insert New Shipper"
           οnclick="InsertShipper" />

    </FORM>
  </BODY>
</HTML>

看下 描红的地方和自动生成的代码有区别吧?  对了 就是这的问题  只要我们定义一下 存储过程的名字是从form中来就可以了  它还有很多属性 比如说contral等等 如果你从控件来的话那就要改写为

<asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
 它了啊

还有一个要注意的地方 就是你的sqldatasource.insertcommand一定要在代码中重新定义 除非你的数据库中没有主键 不过没有主键你也生成不了sqldatasource的添加/删除/更新语句

明天接着写 老婆让我去洗衣服 嘿嘿


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值