Asp.net 问号作为查询参数?

Asp.net 问号作为查询参数?,asp.net,webforms,sqldatasource,querystringparameter,Asp.net,Webforms,Sqldatasource,Querystringparameter,我在上偶然发现了一个数据源示例 代码如下 <%@Page Language="C#" %> <%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.Common" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml

我在上偶然发现了一个数据源示例

代码如下

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<!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 UpdateRecords(Object source, EventArgs e)
{
  // This method is an example of batch updating using a
  // data source control. The method iterates through the rows
  // of the GridView, extracts each CheckBox from the row and, if
  // the CheckBox is checked, updates data by calling the Update
  // method of the data source control, adding required parameters
  // to the UpdateParameters collection.
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add(new Parameter("date",TypeCode.DateTime,DateTime.Now.ToString()));
      MyAccessDataSource.UpdateParameters.Add(new Parameter("orderid",TypeCode.String,oid));
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

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

      <asp:SqlDataSource
        id="MyAccessDataSource"
        runat="server"
        ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>"
        ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>"
        SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?"
        UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?">
        <SelectParameters>
          <asp:QueryStringParameter Name="empId" QueryStringField="empId" />
        </SelectParameters>
      </asp:SqlDataSource>

      <asp:GridView
        id ="GridView1"
        runat="server"
        DataSourceID="MyAccessDataSource"
        AllowPaging="True"
        PageSize="10"
        AutoGenerateColumns="False">
          <columns>
            <asp:TemplateField HeaderText="">
              <ItemTemplate>
                <asp:CheckBox runat="server" />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Order" DataField="OrderID" />
            <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
            <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" />
            <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" />
          </columns>
      </asp:GridView>

      <asp:Button
        id="Button1"
        runat="server"
        Text="Update the Selected Records As Shipped"
        OnClick="UpdateRecords" />

      <asp:Label id="Label1" runat="server" />

    </form>
  </body>
</html>

私有void更新记录(对象源、事件参数)
{
//此方法是使用
//数据源控件。该方法遍历行
//在GridView中,从行中提取每个复选框,如果
//选中该复选框后,通过调用更新来更新数据
//数据源控制的方法,添加所需的参数
//到UpdateParameters集合。
复选框cb;
foreach(此.GridView1.Rows中的GridViewRow行){
cb=(复选框)行。单元格[0]。控件[1];
如果(cb.选中){
字符串oid=(字符串)行。单元格[1]。文本;
MyAccessDataSource.UpdateParameters.Add(新参数(“date”,TypeCode.DateTime,DateTime.Now.ToString());
添加(新参数(“orderid”,TypeCode.String,oid));
MyAccessDataSource.Update();
MyAccessDataSource.UpdateParameters.Clear();
}
}
}
ASP.NET示例
我试过这个代码。但是服务器返回

“?”附近的语法不正确。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.Data.SqlClient.SqlException:靠近“?”的语法不正确。

EmployeeID=?
ShippedDate=?
OrderID=?
SelectCommand
UpdateCommand
中指定了什么? 它们写得好吗


如何使此代码工作?

一些数据库系统使用匿名参数,通常由
发出信号。然后必须按正确的顺序提供参数

其他系统使用命名参数。SqlServer使用
@name
语法。然后您可以无序地提供参数,因为它们是在名称而不是位置上匹配的


WHERE EmployeeID=?
更改为
WHERE EmployeeID=@empId
,以匹配参数的名称。

这三个参数应替换为

EmployeeID = @empId
ShippedDate = @date
OrderID = @ordered

MSDN的样本有时不起作用。

这是否意味着样本写得不好?我已经更改了EmployeeId=@empId,这很好。我该如何处理“ShippedDate=?”和“OrderID=?”@user1978421您应该以类似的方式命名参数。请注意,您还没有它们的参数定义。该示例以Access数据库为目标,该数据库对参数使用