在 table 名称为文本框文本的位置插入查询
Insert query where table name is textbox text
我在将数据插入 table 的 button1 单击事件中遇到问题,table 名称由 textbox1
中的任何文本确定
应该是这样的意思:
tablename = textbox1.text;
sql = "INSERT INTO tablename ([itemserial], [itemname], [itemcount],[itemimage]) VALUES (@itemserial, @itemname, @itemcount, @itemimage)";
像这样更改您的查询
sql = "INSERT INTO "+tablename+" ([itemserial],[itemname],[itemcount],[itemimage]) VALUES (@itemserial,@itemname,@itemcount,@itemimage)";
tablename = textbox1.text;
sql = string.Format("INSERT INTO {0} ([itemserial],[itemname],[itemcount],[itemimage])VALUES(@itemserial,@itemname,@itemcount,@itemimage)", tablename);
虽然我强烈建议不要这样做,因为它允许人们在该文本框中输入他们想要的任何内容。类似于:
罗伯特;删除 TABLE 学生;--
这里有更详细的讨论:
How does the SQL injection from the "Bobby Tables" XKCD comic work?
拥有一个包含您 table 姓名的文本框具有挑战性,因为您在处理此值时应格外小心。您应该对此文本框值实施某种检查。如果您的用户键入的 table 确实存在,一个可能的解决方案是检查您的数据库模式。
您没有告诉我们您使用的是哪个数据库系统,所以我将展示一个使用 Sql Server
的示例
string tableName = textbox1.text;
using(SqlConnection cnn = new SqlConnection(... connectionstring...))
{
cnn.Open();
DataTable dt = cnn.GetSchema("TABLES");
DataRow[] rows = dt.Select("TABLE_NAME = '" + tableName + "'");
if(rows.Length > 0)
{
// Now you are sure to have a valid table in your textbox
// and could use the input value without risking an Sql Injection
string sql = "INSERT INTO [" + tableName + "] ([itemserial]," +
"[itemname],[itemcount],[itemimage]) " +
"VALUES(@itemserial,@itemname,@itemcount,@itemimage)";
.... the remainder of your code that use the query above....
}
else
MessageBox.Show("Please enter a valid name for your table");
扩展此方法,您可以将 TextBox 更改为 ComboBox,并将 ComboBoxStyle 设置为 DropDownList(以阻止键入),并使用上面 GetSchema 调用返回的名称填充 ComboBox....
我在将数据插入 table 的 button1 单击事件中遇到问题,table 名称由 textbox1
中的任何文本确定应该是这样的意思:
tablename = textbox1.text;
sql = "INSERT INTO tablename ([itemserial], [itemname], [itemcount],[itemimage]) VALUES (@itemserial, @itemname, @itemcount, @itemimage)";
像这样更改您的查询
sql = "INSERT INTO "+tablename+" ([itemserial],[itemname],[itemcount],[itemimage]) VALUES (@itemserial,@itemname,@itemcount,@itemimage)";
tablename = textbox1.text;
sql = string.Format("INSERT INTO {0} ([itemserial],[itemname],[itemcount],[itemimage])VALUES(@itemserial,@itemname,@itemcount,@itemimage)", tablename);
虽然我强烈建议不要这样做,因为它允许人们在该文本框中输入他们想要的任何内容。类似于:
罗伯特;删除 TABLE 学生;--
这里有更详细的讨论: How does the SQL injection from the "Bobby Tables" XKCD comic work?
拥有一个包含您 table 姓名的文本框具有挑战性,因为您在处理此值时应格外小心。您应该对此文本框值实施某种检查。如果您的用户键入的 table 确实存在,一个可能的解决方案是检查您的数据库模式。
您没有告诉我们您使用的是哪个数据库系统,所以我将展示一个使用 Sql Server
的示例string tableName = textbox1.text;
using(SqlConnection cnn = new SqlConnection(... connectionstring...))
{
cnn.Open();
DataTable dt = cnn.GetSchema("TABLES");
DataRow[] rows = dt.Select("TABLE_NAME = '" + tableName + "'");
if(rows.Length > 0)
{
// Now you are sure to have a valid table in your textbox
// and could use the input value without risking an Sql Injection
string sql = "INSERT INTO [" + tableName + "] ([itemserial]," +
"[itemname],[itemcount],[itemimage]) " +
"VALUES(@itemserial,@itemname,@itemcount,@itemimage)";
.... the remainder of your code that use the query above....
}
else
MessageBox.Show("Please enter a valid name for your table");
扩展此方法,您可以将 TextBox 更改为 ComboBox,并将 ComboBoxStyle 设置为 DropDownList(以阻止键入),并使用上面 GetSchema 调用返回的名称填充 ComboBox....