SQLBulkCopy - WriteToServer方法不起作用

时间:2013-07-25 12:10:20

标签: c# datatable sqlbulkcopy

我正在尝试将一些数据从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文件填充数据表,一切顺利。该程序运行没有问题但不幸的是没有插入我的表。有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

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

相关问题