如何将Excel中的数据插入数据库

时间:2014-12-04 03:26:53

标签: c# sql datagridview

我在这方面已经打了一段时间,这是以导入形式,它将excel电子表格中的行导入到datagridview,然后允许用户在需要时确保更改,然后添加或更新db行。 / p>

我已经在电子表格中包含了示例行,以及检查这些行的代码部分。我的问题是,其中4行没有标记为prev。无论你运行多少次都会发现它。

任何输入?

示例excel文件:

+-------+----------+-----------+------------------+-------+------------+--------+------------------------+
| Trans | Eff Date | Policy #  |   Insured Name   |  PIF  |    GWP     | Agent% |     Agent NB Comm      |
+-------+----------+-----------+------------------+-------+------------+--------+------------------------+
| AV    | Jul-10   | 186990710 | FLORECE, BOB     | 0     | 0.00       | 10.000 | 0.00                   |
| AV    | Jul-18   | 193214729 | CIRONIDA, JOHN A | 0     | 0.00       | 10.000 | 0.00                   |
| CH    | Jun-26   | 186990710 | FLORECE, BOB     | 0     | 51.97      | 10.000 | 5.20                   |
| CH    | Jul-10   | 186990710 | FLORECE, BOB     | 0     | (64.83)    | 10.000 | (6.48) - NOT MATCHED   |
| CH    | Jul-10   | 186990710 | FLORECE, BOB     | 1     | 272.19     | 10.000 | 27.22                  |
| CH    | Jul-01   | 192182230 | URZ, SUSAN A     | 0     | (50.70)    | 10.000 | (5.07) - NOT MATCHED   |
| CH    | Jul-09   | 193089995 | STOMPKINS, JIM   | 0     | 45.37      | 10.00  | 4.54                   |
| CH    | Jul-18   | 193214729 | CIRONIDA, JOHN A | 1     | 125.99     | 10.000 | 12.60                  |
| CH    | May-15   | 196666151 | STROP, JONA      | 0     | 13.40      | 10.000 | 1.34                   |
| CH    | May-15   | 196666151 | STROP, JONA      | 0     | (13.40)    | 10.000 | (1.34)                 |
| CN    | Jul-03   | 193435083 | MADDORE, SHARON  | -1    | (1,097.81) | 10.000 | (109.78) - NOT MATCHED |
| NB    | Jul-01   | 192182230 | URZ, SUSAN A     | 3     | 570.10     | 10.000 | 57.01                  |
| CH    | Jul-10   | 197907782 | LANSKY, WAYNE    | 0     | 4.34       | 10.000 | 0.43                   |
| NB    | Jul-14   | 184221262 | SMITH, DAN       | 2     | 419.60     | 10.000 | 41.96                  |
| CH    | Jul-03   | 184532495 | MULLEN, ROBERT   | 0     | (16.41)    | 10.000 | (1.64) - NOT MATCHED   |
+-------+----------+-----------+------------------+-------+------------+--------+------------------------+

代码:

        intDGRow = dgvImported.Rows.Count;
        for (int i = 0; i < intDGRow; i++)
        {
            //set var values from the dgv
            strPolicyNum = dgvImported.Rows[i].Cells["policynum"].Value.ToString().Replace(" ", string.Empty);
            strEDate = DateTime.Parse(dgvImported.Rows[i].Cells["EffDate"].Value.ToString()).ToShortDateString();
            strGWP = dgvImported.Rows[i].Cells["gwp"].Value.ToString().Replace(",", string.Empty);
            strPIF = dgvImported.Rows[i].Cells["pif"].Value.ToString();



            //check if row matches a current transaction in the DB - if so update matched column to value = matched - checks for match by doing a row count in the DB that match the fields - if only 1 row matches then the transaction is a match
            if (dbAccess.RowCount("sales", "policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND pif = " + strPIF + " AND gwp = " + strGWP) == 1)
            {
                if (dbAccess.RowCount("sales", "policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND pif = " + strPIF + " AND gwp = " + strGWP + "AND matched = true") == 1)
                {
                    dgvImported.Rows[i].Cells["Matched"].Value = "PrevMatched"; //update the matched column to reflect this item was previously matched
                }
                else
                {
                    dgvImported.Rows[i].Cells["Matched"].Value = "Matched"; //update the matched column to reflect this item was matched
                }//end sub if / else (prev matched)

            }//end if

            else
            {

                dgvImported.Rows[i].Cells["Matched"].Value = "UnMatched"; //update the matched column to reflect this item was not matched
            }//end else


            dgvImported.Rows[i].Cells["producer"].Value = dbAccess.ValueLookup("SELECT producer FROM sales WHERE policynum = '" + strPolicyNum + "'", "producer"); //lookup the producer for the policy number - should pull the last producer attached to the policy

            dgvImported.Rows[i].Cells["SalesID"].Value = dbAccess.ValueLookup("SELECT saleskey FROM sales WHERE policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND gwp = " + strGWP, "saleskey"); //lookup the saleskey from sales table that matches the transaction


        }//end for

    }//end if file open

}//end file open button

如果您需要任何其他详细信息,请与我们联系。

0 个答案:

没有答案