从excel导入数据到数据库在获取空值后不会从excel复制所有数据

时间:2018-04-17 06:39:35

标签: sql-server excel

请帮帮我。建议任何能帮助我解决问题的人,请你乞求你。我差不多1周就遇到了这个问题,我在google上搜索了整整4天。 请帮我解决这个问题。

当我从excel导入数据时,如下所示: Excel file

我只得到第3,4,5行,其余第15行我只得到更长的时间" --------"符号和下面的其余部分为空。 我已经搜索了几乎5天的确切代码,这些代码将复制所有记录或数据,并将其保存到数据库中,并且数据库中列的数据类型都是nvarchar(255)

我用于将数据从excel导入数据库的代码如下:

public void ImportDataFromExcel(string excelFilePath, string user)
    {
        string ssqltable = "tmp_100_POEntry_SF";

        //string myexceldataquery = "select PONO,PODATE,PORECEIVEDDATE,DELIVERYDATE,PARTCODE,QUANTITY,UNITCOST,TOTALAMOUNT,STOCKS,INCHARGE,CLIENT from [Sheet1$]";
        string myexceldataquery = "select [PO NO],[PART CODE],QUANTITY,[UNIT COST],[TOTAL AMOUNT],STOCKS,[IN CHARGE],CLIENT,'5/3/2017' AS PODATE, '5/3/2017' AS PORECEIVEDDATE, '5/3/2017' AS DELIVERYDATE from [Sheet1$]";
        try
        {
            string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelFilePath +
            ";extended properties=" + "\"excel 8.0;hdr=yes;\"";

            string sclearsql = "delete from tmp_100_POEntry_SF where username = @username";
            SqlConnection sqlconn = new SqlConnection(Common.connectionstring);
            SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
            sqlcmd.Parameters.AddWithValue("@username", user);
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn.Close();

            OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
            OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
            oledbconn.Open();
            OleDbDataReader dr = oledbcmd.ExecuteReader();
            SqlBulkCopy bulkcopy = new SqlBulkCopy(Common.connectionstring);
            bulkcopy.DestinationTableName = ssqltable;
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    bulkcopy.DestinationTableName = ssqltable;
                    bulkcopy.ColumnMappings.Add("[PO NO]", "PONO");
                    bulkcopy.ColumnMappings.Add("[PART CODE]", "PARTCODE");
                    bulkcopy.ColumnMappings.Add("QUANTITY", "QUANTITY");
                    bulkcopy.ColumnMappings.Add("[UNIT COST]", "UNITCOST");
                    bulkcopy.ColumnMappings.Add("[TOTAL AMOUNT]", "TOTALAMOUNT");
                    bulkcopy.ColumnMappings.Add("STOCKS", "STOCKS");
                    bulkcopy.ColumnMappings.Add("[IN CHARGE]", "INCHARGE");
                    bulkcopy.ColumnMappings.Add("CLIENT", "CLIENT");
                    bulkcopy.ColumnMappings.Add("PODATE", "PODATE");
                    bulkcopy.ColumnMappings.Add("PORECEIVEDDATE", "PORECEIVEDDATE");
                    bulkcopy.ColumnMappings.Add("DELIVERYDATE", "DELIVERYDATE");

                    bulkcopy.WriteToServer(dr);
                }
            }
            else
            {
                MessageBox.Show("No records found in Excel data!");
            }
            dr.Close();
            string sclearsqls = "UPDATE tmp_100_POEntry_SF SET USERNAME = @username";
            SqlConnection sqlconns = new SqlConnection(Common.connectionstring);
            SqlCommand sqlcmds = new SqlCommand(sclearsqls, sqlconn);
            sqlcmds.Parameters.AddWithValue("@username", user);
            sqlconn.Open();
            sqlcmds.ExecuteNonQuery();
            sqlconn.Close();
            oledbconn.Close();



        }
        catch (Exception ex)
        { throw ex; }
    }

0 个答案:

没有答案