简体   繁体   English

插入数据库时​​如何更改组合框值?

[英]How to change a combobox Value when inserting in database?

I have created a combobox with items. 我创建了一个包含项目的组合框。

Visually in my form the items texts will apear, and i want to change the values to numbers when i insert in the database. 视觉上以表格的形式出现项目文本,当我插入数据库时​​,我想将值更改为数字。

example : 例如:

        private void contact(){
        TypeAdrCmBx.Items.Add("NPAI");
        TypeAdrCmBx.Items.Add("Personnelle");
        TypeAdrCmBx.Items.Add("Professionelle");
        TypeAdrCmBx.Items.Add("Vacances"); }

this is my insert Query : 这是我的插入查询:

                string Query2 = "INSERT INTO [dbo].[Adresses] ([Type] ,[Adresse0],[Adresse1],[Adresse2],[CPT],[Ville],[Pays]) VALUES ('" + this.TypeAdrCmBx.Text + "','" + this.AdrTxtBx0.Text + "','" + this.AdrTxtBx1.Text + "','" + this.AdrTxtBx2.Text + "','" + this.CptTxtBx.Text + "','" + this.VilleTxtBx.Text + "','" + this.PaysCmBx0.Text + "')";

in the database the combobox values are inserted as numbers : 在数据库中,组合框值以数字形式插入:

 "NPAI" = 1
 "Personnelle" = 2
 "Professionelle" = 3
 "Vacances" = 4

On Database side solution, 在数据库方面的解决方案上,

You need to you 2 tables in database in order to do it. 为此,您需要在数据库中有2个表。 One table will hold strings and values like 一个表将包含字符串和值,例如

X Table X表

ID Number  Value
1    1     "NPAI" 
2    2     "Personnelle"
3    3     "Professionelle"
4    4     "Vacances"

and other table you will only use the numbers. 和其他表格,您将只使用数字。 When you want to read them, u will use left join. 当您想阅读它们时,您将使用左联接。

On c# side solution. 在C#方面的解决方案。

You can basicly convert them with if, 您基本上可以将它们转换为

string a="";
if (TypeAdrCmBx.Text.Equals("NPAI"))
    a="1";
else if (TypeAdrCmBx.Text.Equals("Personnelle"))
    a="2";

And then simply make the insertion with a. 然后只需用a插入即可。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM