0

我想跟踪上传 Excel 文件并将其导入我的 SQL 数据库后保存了多少记录。请问有人可以修改我的代码,以便它可以显示存储在我的表中的记录数以及“成功”或“失败”消息吗?

这是我的代码:

protected void btnSend_Click(object sender, EventArgs e)  {
    //file upload path
    string path = fileuploadExcel.PostedFile.FileName;
    //Create connection string to Excel work book
    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\xxxxxx\Desktop\1-8-13-ct.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False";
    //Create Connection to Excel work book
    OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
    //Create OleDbCommand to fetch data from Excel
    OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]",excelConnection);
    excelConnection.Open();
    OleDbDataReader dReader;
    dReader = cmd.ExecuteReader();
    SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
    //Give your Destination table name
    sqlBulk.DestinationTableName = "contact";
    sqlBulk.WriteToServer(dReader);
    excelConnection.Close();
}
4

3 回答 3

1

您可以使用 DataTable 而不是 DataReader,这样您就可以预先确定要写入的行数。例如:

    protected void btnSend_Click(object sender, EventArgs e)
    {
    //file upload path
    string path = fileuploadExcel.PostedFile.FileName;
    //Create connection string to Excel work book
    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\xxxxxx\Desktop\1-8-13-ct.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False";
    //Create Connection to Excel work book
    OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
    //Create OleDbCommand to fetch data from Excel
    OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]",excelConnection);
    excelConnection.Open();
    OleDbDataReader dReader;
   // Datatable table = new DataTable();
     DataTable table = new DataTable();
    dReader = cmd.ExecuteReader();
    table.Load(dReader);
    SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
    //Give your Destination table name
    sqlBulk.DestinationTableName = "contact";
    sqlBulk.WriteToServer(table);
    excelConnection.Close();

    int numberOfRowsInserted= table.Rows.Count;// <-- this is what was written.

    string message=string.Format("<script>alert({0});</script>",numberOfRowsInserted);
    ScriptManager.RegisterStartupScript(this, this.GetType(), "scr",message , false);
    }
于 2013-08-01T11:28:02.020 回答
1

您可以通过创建一个新命令来从工作表中读取行数来实现这一点,如下所示:

OleDbCommand cmd1 = new OleDbCommand("select count(*) from [Sheet1$]", excelConnection);
int rows = (int)cmd1.ExecuteScalar();

或者,使用目标数据库表执行相同操作。

于 2013-08-01T11:30:17.480 回答
0

以下 hack(使用反射)是一个选项:

    /// <summary>
    /// Helper class to process the SqlBulkCopy class
    /// </summary>
    static class SqlBulkCopyHelper
    {
        static FieldInfo rowsCopiedField = null;

        /// <summary>
        /// Gets the rows copied from the specified SqlBulkCopy object
        /// </summary>
        /// <param name="bulkCopy">The bulk copy.</param>
        /// <returns></returns>
        public static int GetRowsCopied(SqlBulkCopy bulkCopy)
        {
            if (rowsCopiedField == null)
            {
                rowsCopiedField = typeof(SqlBulkCopy).GetField("_rowsCopied", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
            }

            return (int)rowsCopiedField.GetValue(bulkCopy);
        }
    }

然后按如下方式使用该类:

int rowsCopied = SqlBulkCopyHelper.GetRowsCopied(bulkCopyObjectInYourCode);

希望这可以帮助。

来自:https ://stackoverflow.com/a/4474394/1727357

于 2013-08-01T11:29:40.777 回答