如何解决C#将用户变量错误地发送到MYSQL
这是一小段代码,我将它们组合在一起以测试功能,然后再将类似的代码实现到更大且无关的项目中。我进行了搜索,发现并找到了有效的点点滴滴代码,甚至找到了一个与相同问题非常相似的示例(我已经检查过,并且没有人遇到的相同错误)。也就是说,我仍然没有获得所需的结果。也许我因为看了太久而“看不懂我的代码”,却错过了一些真正基础的东西?任何反馈将不胜感激,以帮助我走出困境!
此代码在表单上实现。每次我按下“添加新行”按钮时,都会将三个文本框的行拖放到表单上。用户应填写方框及其响应(无论他们添加了多少行数据,都应将它们添加到MYSQL中名为“ autolottest”的表中。该表具有3列:“ Make”,“ Color”和“ “名称”。下面的代码在autolottest中呈现以下用户输入:@ make,@ color和@name,无论用户在文本框中输入什么内容,需要注意的是,我没有设置存储过程在MYSQL中...正在尝试将其发送过来而不创建存储过程。
private void btnAddNewRow_Click(object sender,EventArgs e)
{
iteration = 0;
AutoLotTable = new DataTable();
AutoLotTable.Columns.Add("Make",typeof(string));
AutoLotTable.Columns.Add("Color",typeof(string));
AutoLotTable.Columns.Add("Name",typeof(string));
TextBox txt_Make = new TextBox();
this.Controls.Add(txt_Make);
txt_Make.Size = new Size(100,20);
txt_Make.Name = "txt_Make" + iteration.ToString();
txt_Make.Top = leftControl * 24;
txt_Make.Left = 12;
txt_Make.DataBindings.Add("Text",AutoLotTable,"Make");
TextBox txt_Color = new TextBox();
this.Controls.Add(txt_Color);
txt_Color.Size = new Size(100,20);
txt_Color.Name = "txt_Color" + iteration.ToString();
txt_Color.Top = txt_Make.Top;
txt_Color.Left = 116;
txt_Color.DataBindings.Add("Text","Color");
TextBox txt_Name = new TextBox();
this.Controls.Add(txt_Name);
txt_Name.Size = new Size(100,20);
txt_Name.Name = "txt_Make" + iteration.ToString();
txt_Name.Top = txt_Make.Top;
txt_Name.Left = 222;
txt_Name.DataBindings.Add("Text","Name");
for (int i = 0; i<iteration;i++)
{
DataRow row = AutoLotTable.NewRow();
row["Make"] = txt_Make.Text;
row["Color"] = txt_Color.Text;
row["Name"] = txt_Name.Text;
AutoLotTable.Rows.Add(row);
}
MySqlParameter make = new MySqlParameter();
make.ParameterName = "@Make";
make.DbType = DbType.String;
make.Direction = ParameterDirection.Input;
make.Value = txt_Make.Text;
MySqlParameter color = new MySqlParameter();
color.ParameterName = "@Color";
color.DbType = DbType.String;
color.Direction = ParameterDirection.Input;
color.Value = txt_Color.Text;
MySqlParameter name = new MySqlParameter();
name.ParameterName = "@Name";
name.DbType = DbType.String;
name.Direction = ParameterDirection.Input;
name.Value = txt_Name.Text;
MySqlCommand InsertCommand = new MySqlCommand("Insert into autolottest(Make,Color,Name) values (@Make,@Color,@Name)",AutoLotConnection);
InsertCommand.Parameters.AddWithValue("@Make",make);
InsertCommand.Parameters.AddWithValue("@Color",color);
InsertCommand.Parameters.AddWithValue("@Name",name);
InsertCommand.Prepare();
InsertCommand.ExecuteNonQuery();
InsertCommand.Dispose();
iteration++;
leftControl++;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。