本文介绍了sqlbulkcopy无法读取表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从txt文件中执行sqlbulkcopy读取。我得到的错误是无法访问目标表'marketdetails。

连接字符串是否有任何改变但我不这么认为,因为连接正在打开。

请帮助我

 DataTable dt = new DataTable(); 
string line = null;
int i = 0;

使用(StreamReader sr = File.OpenText(C:\\maq \\+ main1.text +\\+ main1.text +。txt) )
{
while((line = sr.ReadLine())!= null)
{
string [] data = line.Split(',');
if(data.Length> 0)
{
if(i == 0)
{
foreach(数据中的var项)
{
dt.Columns.Add(new DataColumn());
}
i ++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
dt.Rows.Add(row);
}
}
}

SqlConnection con = new SqlConnection(@Data Sou rce =。\ SQLEXPRESS; AttachDbFilename = c:\maq \+ main1.text + @\+ main1.text +。mdf; Trusted_Connection = Yes; User Instance = True;);

{
con.Open();
using(SqlBulkCopy copy = new SqlBulkCopy(con))
{
copy.ColumnMappings.Add(0,0);
copy.ColumnMappings.Add(1,1);
copy.ColumnMappings.Add(2,2);
copy.ColumnMappings.Add(3,3);
copy.ColumnMappings.Add(4,4);
copy.ColumnMappings.Add(5,5);
copy.ColumnMappings.Add(6,6);
copy.DestinationTableName =marketdetails;
copy.WriteToServer(dt);
解决方案



I am performing sqlbulkcopy reading from txt file. error I am getting is "cannot access the destination table 'marketdetails".
Is there any thing to change in connection string but I don't think so because the connection is opening.
Please help me

DataTable dt = new DataTable();
           string line = null;
           int i = 0;

           using (StreamReader sr = File.OpenText("C:\\maq\\" + main1.text + "\\" + main1.text + ".txt))
           {
               while ((line = sr.ReadLine()) != null)
               {
                   string[] data = line.Split(',');
                   if (data.Length > 0)
                   {
                       if (i == 0)
                       {
                           foreach (var item in data)
                           {
                               dt.Columns.Add(new DataColumn());
                           }
                           i++;
                       }
                       DataRow row = dt.NewRow();
                       row.ItemArray = data;
                       dt.Rows.Add(row);
                   }
               }
           }

          SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\maq\" + main1.text + @"\" + main1.text + ".mdf;Trusted_Connection=Yes;User Instance=True;");

               {
               con.Open();
               using (SqlBulkCopy copy = new SqlBulkCopy(con))
               {
                   copy.ColumnMappings.Add(0, 0);
                   copy.ColumnMappings.Add(1, 1);
                   copy.ColumnMappings.Add(2, 2);
                   copy.ColumnMappings.Add(3, 3);
                   copy.ColumnMappings.Add(4, 4);
                   copy.ColumnMappings.Add(5, 5);
                   copy.ColumnMappings.Add(6, 6);
                   copy.DestinationTableName ="marketdetails";
                   copy.WriteToServer(dt);
解决方案



这篇关于sqlbulkcopy无法读取表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:49