SQLBulkCopy-WriteToServer方法不工作

本文关键字:工作 方法 SQLBulkCopy-WriteToServer | 更新日期: 2023-09-27 18:24:58

我正试图将一些数据从Excel文件导出到MDF数据库。为此,我使用Excel数据读取器和SQL批量复制:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
namespace BulkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = File.Open(@"C:'Users'maarab'Desktop'List_Personnel 2013.xlsx", FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
            DataTable dt = CreateDataTable();
            string cx = @"Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|'DataTest.mdf;Integrated Security=True;User Instance=True";
            SqlBulkCopy bcp = new SqlBulkCopy(cx, SqlBulkCopyOptions.KeepIdentity);

            bool first = true;
            while (excelReader.Read())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(excelReader.GetString(0)))
                        break;
                    else
                    {
                        string numNat = excelReader.GetString(2);
                        DateTime birthDate = excelReader.GetDateTime(9);
                        DateTime startDate = excelReader.GetDateTime(1);
                        if (!String.IsNullOrWhiteSpace(numNat))
                        {
                            if (numNat.Length == 12)
                            {
                                numNat = numNat.Remove(6, 1);
                            }
                        }
                        if (birthDate.Year < 1753)
                            birthDate = DateTime.Now;
                        if (startDate.Year < 1753)
                            startDate = DateTime.Now;
                        dt.Rows.Add(excelReader.GetString(0), excelReader.GetString(0), numNat, startDate, birthDate);
                    }
                }
            }
            bcp.DestinationTableName = "person";
            bcp.BatchSize = 100;
            bcp.ColumnMappings.Add(0, 1);
            bcp.ColumnMappings.Add(1, 2);
            bcp.ColumnMappings.Add(2, 3);
            bcp.ColumnMappings.Add(3, 4);
            bcp.ColumnMappings.Add(4, 5);
            try
            {
                bcp.WriteToServer(dt, DataRowState.Unchanged);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }

        public static DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Columns.Add("NumNat", typeof(string));
            dt.Columns.Add("Birthdate", typeof(DateTime));
            dt.Columns.Add("StartDate", typeof(DateTime));
            return dt;
        }
    }
}

我正在用Excel文件填充数据表,一切都很顺利。程序运行正常,但不幸的是,没有插入到我的表中。有解决这个问题的办法吗?

SQLBulkCopy-WriteToServer方法不工作

如果使用服务器资源管理器查看表,则应仔细检查此窗口中使用的连接。通常情况下,您的数据库文件列在项目文件中,并且服务器资源管理器使用的连接指向项目文件夹中的该文件
当然,当您的程序运行时,使用的连接是在app.config中指定的连接。
在这种情况下,您没有错误(因为不存在错误),但您使用服务器资源管理器查找错误的数据库,在该数据库中没有发生任何操作,也看不到更新
解决方案很简单,您需要重新配置服务器资源管理器使用的连接,以指向|DataDirectory|替换字符串的有效值(通常为BIN''DEBUG或BIN,具体取决于项目类型)