C# 我想插入另一个表列中的值
我有两张这样的桌子 tbl_电机C# 我想插入另一个表列中的值,c#,mysql,sql,C#,Mysql,Sql,我有两张这样的桌子 tbl_电机 motor_id motor_types -------------------------- 1 audi 2 Ferrari tbl_员工 employee_id employee_name motor_id -------------------------------------- 1 jack 2 2 john
motor_id motor_types
--------------------------
1 audi
2 Ferrari
tbl_员工
employee_id employee_name motor_id
--------------------------------------
1 jack 2
2 john 1
但我有问题。我在我的注册表上使用了组合框。我的组合框查询是
从tbl\U电机中选择电机类型
如何将新值插入到tbl_employee
中,我的组合框是string
嗯,我的意思是将字符串解析为int。A
组合框可以包含任何类型的对象。应避免将添加文本设置为项目。增加你的马达
您可能有这样一个类:
级电机
{
public int MotorId{get;set;}
公共字符串类型{get;set;}
public override string ToString()//必须重写。这将用于显示组合框中的文本
{
返回此.MotorType;
}
}
这就是如何添加电机
并阅读选定的编辑项:
// This is your Array which you got from your DB
Motor[] motors = new Motor[]
{
new Motor() { MotorId = 1, MotorType = "Audi" },
new Motor() { MotorId = 2, MotorType = "Ferrari" },
};
// We clear old items and add the motors:
this.comboBox1.Items.Clear();
this.comboBox1.Items.AddRange(motors);
// Select something for demonstration
this.comboBox1.SelectedIndex = 1;
// Read the selected item out of the combobox
Motor selectedMotor = this.comboBox1.SelectedItem as Motor;
// Let's have a look what we got
MessageBox.Show(selectedMotor.MotorId + ": " + selectedMotor.MotorType);
“嗯,我的意思是将字符串解析为int…”?这应该是关于程序逻辑部分的问题。确保您的组合框使用带有ID的对象,而不仅仅是文本。正如@juergend所说,使用ID和文本填充组合框。例如:audi
提交到服务器时,在插入tbl_employee时,您将拥有要引用的id。我只是按照本教程中的步骤操作,Im无法将其(对象id)实现到mysql中。相关:和