2013-09-24 29 views
0

我在http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.querystringparameter(v=vs.100).aspx問號作爲查詢參數?

在一個DataSource樣品絆倒的代碼如下

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

我曾嘗試此代碼。但服務器返回

'?'附近語法不正確。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.Data.SqlClient.SqlException:'?'附近的語法錯誤。

什麼EmployeeID=?ShippedDate=?OrderID = ?SelectCommandUpdateCommand指定? 它們是否正確書寫?

如何使此代碼工作?

回答

0

這三個參數應與

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

MSDN的樣品進行更換,有時不工作。

2

某些數據庫系統使用匿名參數,通常由?發信號通知。然後您必須以正確的順序提供參數。

其他系統使用命名參數。 SqlServer使用@name語法。然後,您可以不按順序提供參數,因爲它們在名稱而不是位置上匹配。

WHERE EmployeeID=?更改爲WHERE [email protected]以匹配參數的名稱。

+0

這是否意味着樣本寫得不好? –

+0

我已更改EmployeeId = @ empId,這很好。我該如何處理'ShippedDate =?'和'OrderID =?' –

+1

@ user1978421您應該以類似的方式命名參數。請注意,您還沒有參數定義。該示例針對Access數據庫,它使用'?'作爲參數。 –