从SQL获取数据连同读取在C#中脱颖而出

问题描述 投票:0回答:2

我有一个程序,能够导入Excel文件,数据表结合使用datagridview的。目前,我需要添加其他列名InvtID,并从SQL Server的基础上,我已导入条码列(列“3”为头),这样InvtID数据。

我怎么能因为现在我的编码都能够得到数据后导入Excel来实现这一目标之一,所以InvtID列数据不是并排侧条码列。这是我遇到的完成这项任务的唯一问题,请帮助我。谢谢

在这里我有多么的DataTable的样子photo

public void filldatagridview(ExcelWorksheet workSheet)
       {
           DataTable dt = new DataTable();

           //Create the data column
           for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
           {
               dt.Columns.Add(col.ToString());
           }

          for (int row = 12; row <= 26; row++)
           {
               DataRow newRow = dt.NewRow(); //Create a row
               int i = 0;
               for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
               {
                   newRow[i++] = workSheet.Cells[row, col].Text;
               }
               dt.Rows.Add(newRow);
           }

           dt.Columns.RemoveAt(0); //remove No
           dt.Columns.RemoveAt(0); //remove article

      //Get BookCode
      using (SqlConnection conn = new SqlConnection("Server"))
      using (SqlCommand cmd = new SqlCommand(null, conn))
     {
    StringBuilder sb = new StringBuilder("WITH cte AS(SELECT case WHEN InvtID IS NULL OR InvtID='' THEN 'No Bookcode Found' ELSE InvtID END AS InvtID,Barcode,ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rid=1 and Barcode In (");
             for (int i = 0; i < dt.Rows.Count; i++)
              {
                 if (i != 0) sb.Append(",");
                  string name = "@P" + i;
                  cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
                  sb.Append(name);
              }
              sb.Append(")");
              cmd.CommandText = sb.ToString();
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);

              dt.Columns["BOOKCODE"].SetOrdinal(0);
              dataGridView2.DataSource = dt;
           }
      }
c# sql-server epplus
2个回答
1
投票

我不知道这是工作。

避免添加新行时行已经存在。

dt DataTable第一添加列

只要看看这段代码:

 public void filldatagridview(ExcelWorksheet workSheet)
        {
            DataTable dt = new DataTable();
            DataTable dtInvtID = new DataTable();
            dt.Columns.Add("dtInvtID"); //ADDING NEW COLUMN FIRST FOR YOUR dtInvtID
            //Create the data column
            for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
            {
                dt.Columns.Add(col.ToString());
            }

            for (int row = 12; row <= 26; row++)
            {
                DataRow newRow = dt.NewRow(); //Create a row
                int i = 0;
                for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
                {
                    newRow[i++] = workSheet.Cells[row, col].Text;
                }
                dt.Rows.Add(newRow);
            }

            dt.Columns.RemoveAt(0); //remove No
            dt.Columns.RemoveAt(0); //remove article

            //Get BookCode
            using (SqlConnection conn = new SqlConnection("Server"))
            using (SqlCommand cmd = new SqlCommand(null, conn))
            {
                StringBuilder sb = new StringBuilder("WITH cte AS(SELECT case WHEN InvtID IS NULL OR InvtID='' THEN 'No Bookcode Found' ELSE InvtID END AS InvtID,Barcode,ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rid=1 and Barcode In (");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i != 0) sb.Append(",");
                    string name = "@P" + i;
                    cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
                    sb.Append(name);
                }
                sb.Append(")");
                cmd.CommandText = sb.ToString();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dtInvtID);

                dt.Columns["BOOKCODE"].SetOrdinal(0);

            }


            int dtctr = 0;
            int ctr = 0;
            foreach (DataRow dr in dt.Rows)//inserting the value of your InvtID to dt.Rows
            {
                dtctr += 1;
                ctr = 1;
                foreach (DataRow InvtID in dtInvtID.Rows) //Getting the value of dtInvtID from database
                {
                    if (ctr == dtctr)//Condition when the row position is equal (dt.Rows==dtInvtID.Rows) IF THIS NOT RETURN A REAL POSITION THEN YOU CAN RUN IT IN DEBUG MODE T CHECK
                    {
                        dr["dtInvtID"] = InvtID[0];
                        ctr += 1;
                        break;
                    }
                    ctr += 1;
                }
            }

            dataGridView2.DataSource = dt;
        }

提醒:

此代码还没有进行测试。任何错误的结果,您可以检查它在调试模式下运行。

我已经看到了一些有趣的问题:

for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i != 0) sb.Append(",");
                    string name = "@P" + i;
                    cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
                    sb.Append(name);
                }
                sb.Append(")");

尝试修改它,因为从上面我的回答是越来越数据库的价值。这可能会导致冗余。


0
投票

你第一次插入新行到表中的数据与此代码。

 dt.Rows.Add(newRow);

第二次你正在使用的适配器,以填补与下面的代码相同的数据表。

da.Fill(dt);

这在端部,而不是在数据表中更新现有的行插入的行。为了将数据对齐到您需要先填写其他的数据表通过一个适配器,然后使用循环和匹配数据来更新现有的或原始数据表编写代码的数据表。

© www.soinside.com 2019 - 2024. All rights reserved.