ParameterCollection.Add 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 Parameter 物件加入至集合。
多載
Add(Parameter) |
將指定的 Parameter 物件附加到集合的結尾。 |
Add(String, String) |
以指定的名稱和預設值建立 Parameter 物件,並將其附加至集合結尾。 |
Add(String, DbType, String) |
使用指定的名稱、資料庫型別及預設值建立 Parameter 物件,並將它加入集合的結尾。 |
Add(String, TypeCode, String) |
Add(Parameter)
將指定的 Parameter 物件附加到集合的結尾。
public:
int Add(System::Web::UI::WebControls::Parameter ^ parameter);
public int Add (System.Web.UI.WebControls.Parameter parameter);
member this.Add : System.Web.UI.WebControls.Parameter -> int
Public Function Add (parameter As Parameter) As Integer
參數
傳回
加入項目的索引值。
範例
下列程式碼範例示範如何使用 AccessDataSource 控制項和 FormParameter 物件,在 控制項中 GridView 顯示來自 Microsoft Access 資料庫的資訊。 物件 FormParameter 會使用 Add(Parameter) 方法新增至 SelectParameters 集合。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@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">
void Page_Load(Object sender, EventArgs e){
// You can add a FormParameter to the AccessDataSource control's
// SelectParameters collection programmatically.
AccessDataSource1.SelectParameters.Clear();
// Security Note: The AccessDataSource uses a FormParameter,
// Security Note: which does not perform validation of input from the client.
// Security Note: To validate the value of the FormParameter,
// Security Note: handle the Selecting event.
FormParameter formParam = new FormParameter("lastname","LastNameBox");
formParam.Type=TypeCode.String;
AccessDataSource1.SelectParameters.Add(formParam);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataSet"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
FROM Orders WHERE EmployeeID =
(SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
</asp:accessdatasource>
<br />Enter the name "Davolio" or "King" in the text box and click the button.
<br />
<asp:textbox
id="LastNameBox"
runat="server" />
<br />
<asp:button
id="Button1"
runat="server"
text="Get Records" />
<br />
<asp:gridview
id="GridView1"
runat="server"
allowsorting="True"
datasourceid="AccessDataSource1">
</asp:gridview>
</form>
</body>
</html>
<%@Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
' You can add a FormParameter to the AccessDataSource control's
' SelectParameters collection programmatically.
AccessDataSource1.SelectParameters.Clear()
' Security Note: The AccessDataSource uses a FormParameter,
' Security Note: which does not perform validation of input from the client.
' Security Note: To validate the value of the FormParameter,
' Security Note: handle the Selecting event.
Dim formParam As New FormParameter("lastname","LastNameBox")
formParam.Type=TypeCode.String
AccessDataSource1.SelectParameters.Add(formParam)
End Sub ' Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataSet"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
FROM Orders WHERE EmployeeID =
(SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
</asp:accessdatasource>
<br />Enter the name "Davolio" or "King" in the text box and click the button.
<br />
<asp:textbox
id="LastNameBox"
runat="server" />
<br />
<asp:button
id="Button1"
runat="server"
text="Get Records" />
<br />
<asp:gridview
id="GridView1"
runat="server"
allowsorting="True"
datasourceid="AccessDataSource1">
</asp:gridview>
</form>
</body>
</html>
備註
Add(Parameter)使用 方法,將 物件附加 Parameter 至集合結尾。 這個 方法的實作會採用 Parameter 參數所指定的 物件, param
並將它附加至集合。
另請參閱
適用於
Add(String, String)
以指定的名稱和預設值建立 Parameter 物件,並將其附加至集合結尾。
public:
int Add(System::String ^ name, System::String ^ value);
public int Add (string name, string value);
member this.Add : string * string -> int
Public Function Add (name As String, value As String) As Integer
參數
- name
- String
參數名稱。
- value
- String
字串,做為參數的預設值。
傳回
加入項目的索引值。
範例
下列程式碼範例示範如何使用 Add(String, String) 方法,藉由提供 name
和 value
參數,將新的 Parameter 物件 ParameterCollection 加入集合。 在此範例中, Parameter 物件會新增至 Access 資料來源控制項的 Update 命令,該控制項系結至控制項的值 TextBox 。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
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("date", TypeCode.DateTime, DateTime.Now.ToString());
MyAccessDataSource.UpdateParameters.Add("orderid", oid);
MyAccessDataSource.Update();
MyAccessDataSource.UpdateParameters.Clear();
}
}
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)
Dim cb As CheckBox
Dim row As GridViewRow
For Each row In GridView1.Rows
cb = CType(row.Cells(0).Controls(1), CheckBox)
If cb.Checked Then
Dim oid As String
oid = CType(row.Cells(1).Text, String)
MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())
MyAccessDataSource.UpdateParameters.Add("orderid", oid)
MyAccessDataSource.Update()
MyAccessDataSource.UpdateParameters.Clear()
End If
Next
End Sub ' UpdateRecords
</script>
備註
Add(String, String)使用 方法來建立物件,並將預設值附加 Parameter 至集合結尾。 這個 方法的實作會 Parameter 分別使用 和 value
參數所 name
指定的名稱和預設值來建立 物件,並將它附加至集合。
另請參閱
適用於
Add(String, DbType, String)
使用指定的名稱、資料庫型別及預設值建立 Parameter 物件,並將它加入集合的結尾。
public:
int Add(System::String ^ name, System::Data::DbType dbType, System::String ^ value);
public int Add (string name, System.Data.DbType dbType, string value);
member this.Add : string * System.Data.DbType * string -> int
Public Function Add (name As String, dbType As DbType, value As String) As Integer
參數
- name
- String
參數名稱。
- dbType
- DbType
參數的資料庫型別。
- value
- String
參數的預設值。
傳回
加入項目的索引值。
備註
這個方法適用于資料庫類型。 Add(String, TypeCode, String)使用 CLR 類型的 方法。
適用於
Add(String, TypeCode, String)
public:
int Add(System::String ^ name, TypeCode type, System::String ^ value);
public int Add (string name, TypeCode type, string value);
member this.Add : string * TypeCode * string -> int
Public Function Add (name As String, type As TypeCode, value As String) As Integer
參數
- name
- String
參數名稱。
- type
- TypeCode
參數的類型。
- value
- String
參數的預設值。
傳回
加入項目的索引值。
範例
下列程式碼範例示範如何使用 Add(String, TypeCode, String) 方法,藉由提供 name
、 value
和 type
參數,將新 Parameter 物件 ParameterCollection 加入集合。 在此範例中, Parameter 物件會新增至 Access 資料來源控制項的 Update 命令,以提供目前系統時間的值。 參數會以 TypeCodeDateTime 的 加入。
<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
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("date", TypeCode.DateTime, DateTime.Now.ToString());
MyAccessDataSource.UpdateParameters.Add("orderid", oid);
MyAccessDataSource.Update();
MyAccessDataSource.UpdateParameters.Clear();
}
}
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)
Dim cb As CheckBox
Dim row As GridViewRow
For Each row In GridView1.Rows
cb = CType(row.Cells(0).Controls(1), CheckBox)
If cb.Checked Then
Dim oid As String
oid = CType(row.Cells(1).Text, String)
MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())
MyAccessDataSource.UpdateParameters.Add("orderid", oid)
MyAccessDataSource.Update()
MyAccessDataSource.UpdateParameters.Clear()
End If
Next
End Sub ' UpdateRecords
</script>
備註
Add(String, TypeCode, String)使用 方法來建立強型別 Parameter 物件,並將預設值附加至集合結尾。 這個方法的實作會 Parameter 使用 、 type
和 value
參數所 name
指定的名稱、類型和值,分別建立 物件,並將它附加至集合。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應