具有不同数据类型c#的数组

时间:2015-09-24 07:53:26

标签: c# arrays

我正在使用窗体表单应用程序。我有一个数组来存储一个包含不同数据类型的复选框列表(字符串和整数)。下面的代码是我的checkBox checkBox A到E获取员工姓名,cheeckBox 1-5获取员工最喜欢的号码。

//Array of Class and secondary Check Box
CheckBox[] myCheckBoxArray = new CheckBox[10];
myCheckBoxArray[0] = checkBoxA;
myCheckBoxArray[1] = checkBoxB;
myCheckBoxArray[2] = checkBoxC;
myCheckBoxArray[3] = checkBoxD;
myCheckBoxArray[4] = checkBoxE;
myCheckBoxArray[5] = checkBox1;
myCheckBoxArray[6] = checkBox2;
myCheckBoxArray[7] = checkBox3;
myCheckBoxArray[8] = checkBox4;
myCheckBoxArray[9] = checkBox5;

之后我试图将复选框绑定到我的SQL数据库并使用foreach循环来检查是否选中了这些复选框中的任何一个。我想知道这是否是将整数和字符串数组合在一起的好方法,还是应该使用嵌套的foreach循环?因为当我做这样的事情时,我正在努力学习SQL语句。出于某种原因,我得到conversion failed when converting the varchar value 'A' to data type int。我不确定应该如何解决这个问题。

foreach(var selectedItems in myCheckBoxArray){

    if(selectedItems.Checked){                 
        string q = "SELECT * FROM [tb1] as tb1 LEFT JOIN [tb2] as tb2 ON tb1.id = tb2.id 
               LEFT JOIN [tb3] as tb3 ON tb1.eId = tb3.eId
               WHERE employeeName = '" + selectedItems.Text +  //get the name
               "' AND empFavorityNumber = '" + selectedItems.Text + "'" //get the integer
    }
}

NEW EDITED

CheckBox[] myCheckBoxArray = new CheckBox[5];
myCheckBoxArray[0] = checkBoxA;
myCheckBoxArray[1] = checkBoxB;
myCheckBoxArray[2] = checkBoxC;
myCheckBoxArray[3] = checkBoxD;
myCheckBoxArray[4] = checkBoxE;

CheckBox[] mySecCheckBoxArray = new CheckBox[5];
mySecCheckBoxArray[0] = checkBox1;
mySecCheckBoxArray[1] = checkBox2;
mySecCheckBoxArray[2] = checkBox3;
mySecCheckBoxArray[3] = checkBox4;
mySecCheckBoxArray[4] = checkBox5;

foreach(var selectedItems in myCheckBoxArray) {
                    foreach (var secondselectedItemsin  in mySecondCheckBoxArray)
                    {
                        if (checkedItems.Checked)
                        {
                             string q = "SELECT * FROM [tb1] as tb1 LEFT JOIN [tb2] as tb2 ON tb1.id = tb2.id 
               LEFT JOIN [tb3] as tb3 ON tb1.eId = tb3.eId
               WHERE employeeName = '" + selectedItems.Text +  //get the name
               "' AND empFavorityNumber = '" + secondselectedItems.Text + "'" //get the integer
                         }
                      }
  }

0 个答案:

没有答案