简体   繁体   English

ASP.NET/VB.NET:生成插入命令

[英]ASP.NET / VB.NET : Generate Insert Command

I have all of my form variables in my codebehind, they were all retrieved using "Request.Form". 我的代码中都有所有表单变量,它们都是使用“ Request.Form”检索的。

As Far as I Know..If I use the SQLDataSource to do this I have to use ASP.NET Controlls(which I do not want). 据我所知..如果我使用SQLDataSource做到这一点,我必须使用ASP.NET Controlls(我不想要)。

INSERT INTO Orders(FirstName, LastName, Email, PhoneNumber, Address, City, State, ZipCode, PaymentMethod, PromoCode, OrderStatus, Tracking, PPEmail, SubmissionDate) VALUES (@FirstName, @LastName, @Email, @Phone, @Address, @City, @State, 11111, @PaymentMethod', '0', 'New Order - Pending', '0', @PPEMAIL, @Date)

CodeBehind 代码背后

    Dim fPrice As String = CType(Session.Item("Qprice"), String)
    Dim DeviceMake As String = CType(Session.Item("Make"), String)
    Dim PaymentMethod As String = Request.Form("Payment Type")
    Dim DeviceModel As String = CType(Session.Item("Model"), String)
    Dim DeviceCondition As String = CType(Session.Item("Condition"), String)
    Dim SubmissionDate As String = Date.Now.ToString
    Dim FirstName = Request.Form("First")
    Dim LastName = Request.Form("Last")
    Dim City = Request.Form("City")
    Dim Phone = Request.Form("Phone")
    Dim Address = Request.Form("Address")
    Dim State = Request.Form("State")
    Dim Zip = Request.Form("Zip")
    Dim Email = Request.Form("EMail")

Is there a way I can attatch my variables to the insert statment generated by the SQLDatasource, without having to manually code the parameters? 有没有一种方法可以将变量附加到SQLDatasource生成的插入语句,而无需手动编码参数?

You could use FormParameter . 您可以使用FormParameter This doesn't require any ASP.NET control so far I'm aware. 到目前为止,我知道这不需要任何ASP.NET控件。

  <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>

Pay special attention to Microsoft warning in relation to this mechanism 特别注意有关此机制的Microsoft警告

The FormParameter does not validate the value passed by the form element in any way; FormParameter不会以任何方式验证form元素传递的值。 it uses the raw value. 它使用原始值。 In most cases you can validate the value of the FormParameter before it is used by a data source control by handling an event, such as the Selecting, Updating, Inserting, or Deleting event exposed by the data source control you are using. 在大多数情况下,您可以通过处理事件来验证FormParameter的值,以供数据源控件使用它,例如处理所使用的数据源控件公开的Selecting,Updating,Insert或Deleting事件。 If the value of the parameter does not pass your validation tests, you can cancel the data operation by setting the Cancel property of the associated CancelEventArgs class to true. 如果参数的值未通过验证测试,则可以通过将关联的CancelEventArgs类的Cancel属性设置为true来取消数据操作。

Its probably better that you forcibly attach the parameters. 强制附加参数可能更好。 As you are getting your values from Form and Session (ick), parameterizing the SQL will give you some measure of protection. 当您从Form和Session中获取值时(ick),参数化SQL将为您提供一定程度的保护。

You may want to explore code generation (A-la T4 ), this way you can point the codegen at a proc/table and it will generate the vb code to call it, attach the params and execute the statement. 您可能想探索代码生成(A-la T4 ),通过这种方式,您可以将代码生成指向proc /表,它将生成vb代码来调用它,附加参数并执行语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM