【发布时间】:2014-12-01 22:51:00
【问题描述】:
我不明白如何在表单面板中的文本框中捕获值。我正在尝试使用这些值更新我的数据库表。
谁能指出我正确的方向?
代码:
private void btnSupplier_Click(object sender, EventArgs e)
{
try
{
Panel pnlAddSupplier = new Panel();
TextBox txtSupplierID = new TextBox();
TextBox txtSupplierName = new TextBox();
Button btnAddSupplier = new Button();
Label lblSupplierID = new Label();
Label lblSupplierName = new Label();
// Initialize the Panel control.
pnlAddSupplier.Location = new Point(56, 74);
pnlAddSupplier.Size = new Size(200, 200);
// Set the Borderstyle for the Panel to three-dimensional.
pnlAddSupplier.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// Initialize the Label and TextBox controls.
lblSupplierID.Location = new Point(60, 0);
lblSupplierID.Text = "Supplier ID";
lblSupplierID.Size = new Size(104, 20);
txtSupplierID.Location = new Point(20, 40);
txtSupplierID.Text = "";
txtSupplierID.Size = new Size(152, 20);
lblSupplierName.Location = new Point(20, 70);
lblSupplierName.Text = "Supplier Name";
lblSupplierName.Size = new Size(150, 20);
txtSupplierName.Location = new Point(20, 90);
txtSupplierName.Text = "";
txtSupplierName.Size = new Size(152, 20);
lblSupplierID.Location = new Point(16, 16);
lblSupplierID.Text = "Supplier ID";
btnAddSupplier.Location = new Point(60, 120);
btnAddSupplier.Text = "Add";
btnAddSupplier.Click += btnAddSupplier_Click;
// Add the Panel control to the form.
this.Controls.Add(pnlAddSupplier);
// Add the Label and TextBox controls to the Panel.
pnlAddSupplier.Controls.Add(lblSupplierID);
pnlAddSupplier.Controls.Add(txtSupplierID);
pnlAddSupplier.Controls.Add(lblSupplierName);
pnlAddSupplier.Controls.Add(txtSupplierName);
pnlAddSupplier.Controls.Add(btnAddSupplier);
}
catch { }
}
public void btnAdd_Click(object sender, EventArgs e)
{
string username= //how to assign textbox value?;
int age = //how to assign textbox value;
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
string query = "INSERT INTO table(username, age) VALUES ('@xxx', '@xxx') ";
//please not these values are for demonstration.
using (connection)
{
ObjDb.OpenConnection();
ObjDb.ExecuteNonQuery(query, connection);
}
}
如您所见,此代码对 SQL 注入开放,我想使用参数。为此,我需要捕获详细信息输入。如何实现?
我尝试分配 supplierID = txtSupplierID.Text,但收到一条消息说 txtSupplier.Text 不存在。
谢谢。
【问题讨论】: