尝试在 foreach 语句 C# 中跳过一列

我有一个我正在研究的库存管理程序。数据库和表系统的设置方式是,每个类别都是不同的表。这是必要的,因为每个类别(即鞋子、电话、笔记本电脑)都有不同的字段来唯一标识每个产品。因此,我创建了一个表,然后通过 foreach 语句添加名为列的用户。我遇到的问题是它想在 ID 列已经存在时添加它。我尝试添加嵌套在 foreach 中的 If 语句,但它只是不断打破 foreach 循环。我只是还没有想出一种方法让它跳过 ID 列并继续添加每个列。


这是功能代码:


private void btnCreateCategory_Click(object sender, EventArgs e)

{

    List<string> FieldNames = new List<string>();


    foreach (var item in lstFieldNames.Items)

    {

        FieldNames.Add(lstFieldNames.Items.ToString());

    }


    DataTable NewCategory = new DataTable();

    NewCategory = CreateTable(FieldNames);

    dgPreviewAdd.DataSource = NewCategory;


    using (SqlConnection Connection = new SqlConnection(

       Helper.cnnVal("InventoryManager")))

    {

        using (SqlCommand command = new SqlCommand("", Connection))

        {

            command.CommandText = "Create Table tbl" + 

                NewCategory.TableName.ToString() + "(ID int)";


            Connection.Open();

            command.ExecuteNonQuery();

            Connection.Close();


            foreach (DataColumn newColumn in NewCategory.Columns)

            {

                 If( newColumn.ColumnName("ID")

                  {

                  }

                  Else

                  {

                SqlParameter colparam = new SqlParameter();

                colparam.ParameterName = "@ColumnName";

                colparam.Value = newColumn.ColumnName.ToString();


                SqlParameter tblParam = new SqlParameter();

                tblParam.ParameterName = "@TableName";

                tblParam.Value = "tbl" + NewCategory.TableName.ToString();


                command.Parameters.Add(colparam);

                command.Parameters.Add(tblParam);

                command.CommandText = "dbo.AddCategoryColumns";

                command.CommandType = CommandType.StoredProcedure;


                Connection.Open();

                command.ExecuteNonQuery();

                Connection.Close();

                }

            }

        }

    }

}


慕尼黑8549860
浏览 329回答 2
2回答

倚天杖

使用以下任一:foreach (DataColumn newColumn in NewCategory.Columns){&nbsp; &nbsp; if (newColumn.ColumnName == "ID") continue;&nbsp; &nbsp; //Rest of logic goes here}或完全从循环中排除:foreach (DataColumn newColumn in NewCategory.Columns.Where( c => c.ColumnName != "ID" )){&nbsp; &nbsp; //Rest of logic goes here}

湖上湖

您可以使用 distinct 对类别进行分组不要忘记使用 System.Linq 添加;private void btnCreateCategory_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<string> FieldNames = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in lstFieldNames.Items)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FieldNames.Add(lstFieldNames.Items.ToString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var grpfieldNames = FieldNames.Distinct();&nbsp; &nbsp; &nbsp; &nbsp; DataTable NewCategory = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; NewCategory = CreateTable(grpfieldNames);&nbsp; &nbsp; &nbsp; &nbsp; dgPreviewAdd.DataSource = NewCategory;&nbsp; &nbsp; &nbsp; &nbsp; using (SqlConnection Connection = new SqlConnection(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Helper.cnnVal("InventoryManager")))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (SqlCommand command = new SqlCommand("", Connection))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.CommandText = "Create Table tbl" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewCategory.TableName.ToString() + "(ID int)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (DataColumn newColumn in NewCategory.Columns)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If(newColumn.ColumnName("ID")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlParameter colparam = new SqlParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colparam.ParameterName = "@ColumnName";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colparam.Value = newColumn.ColumnName.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlParameter tblParam = new SqlParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tblParam.ParameterName = "@TableName";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tblParam.Value = "tbl" + NewCategory.TableName.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.Parameters.Add(colparam);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.Parameters.Add(tblParam);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.CommandText = "dbo.AddCategoryColumns";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.CommandType = CommandType.StoredProcedure;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadCategory catTableLoad = new LoadCategory();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable catTable = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catTable = catTableLoad.getCategoryTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlParameter param = new SqlParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.ParameterName = "@CategoryName";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.Value = NewCategory.TableName.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlParameter param2 = new SqlParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param2.ParameterName = "@IdNumber";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param2.Value = catTable.Rows.Count + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.Parameters.Add(param);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.Parameters.Add(param2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.CommandText = "dbo.AddCategory";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.CommandType = CommandType.StoredProcedure;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP