2012-01-17 48 views
11

读取一个Excel表(到transferTable),我想这些数据添加到使用SqlBulkCopy的新表(destinationTable会)之后,但我得到的错误:SqlBulkCopy的无法访问表

Cannot access destination table 'test' 

我已经尝试使用默认的表名和使用方括号,但是没有奏效。

有什么建议吗?

private void writeToDBButton_Click(object sender, EventArgs e) { 
    MakeTable(); 
    destinationTable.TableName = "test"; 
    testDBDataSet.Tables.Add("test"); 

    // Connects to the sql-server using Connection.cs 
    SqlConnection connection = Connection.GetConnection(); 

    using (connection) { 
     connection.Open(); 

     // Uses SqlBulkCopy to copy the data from our transferTable to the destinationTable 
     using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { 
      bulkCopy.DestinationTableName = destinationTable.TableName; 

      try { 
       // Write from the source to the destination. 
       bulkCopy.WriteToServer(transferTable); 
       this.dataGridView2.DataSource = destinationTable; 
      } 
      catch (Exception ex) { 
       MessageBox.Show(ex.Message); 
      } 

      connection.Close(); 
     } 
    } 
} 

private void saveDBButton_Click(object sender, EventArgs e) { 
    this.Validate(); 
    this.usersBindingSource.EndEdit(); 
    this.tableAdapterManager.UpdateAll(this.testDBDataSet); 
} 


private void MakeTable() { 
    for (int counter = 0; counter < columns; counter++) { 
     DataColumn dummy = new DataColumn(); 
     dummy.DataType = System.Type.GetType("System.Double"); 
     destinationTable.Columns.Add(dummy); 
    } 
} 
+0

你应该增加更多的相关变量,比如'C#'和数据库,如:'SQL-server','MS-access' - '平方米l'是一个通用标签,'access'几乎没有意义。 – Fionnuala 2012-01-17 12:37:13

回答

5

检查用户连接到数据库具有

GRANT ALTER ON [dbo].[TABLE_XXX] TO [appuser] 

在回答关于MSDN forum建议由Jhilden。

+0

这并没有解决我的问题。我仍然无法访问表,即使用户是数据库所有者 – user3183411 2018-03-07 17:56:08

2

看来,执行此代码的用户没有适当的数据库访问权限。 *检查以便用户有权访问。 *检查用于连接数据库的连接字符串。

+0

我的连接字符串是: string connectionString = @“Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ TestDB.mdf; User Instance = True; Integrated Security = True;”; – SND 2012-01-18 08:58:15

2

我最近遇到了这个相同的错误,并在Google上搜索答案时遇到了这篇文章。我能够通过给予正在执行批量复制命令插入的用户并在目标表上选择权限来解决问题。 最初我只向用户授予了插入权限,并得到'无法访问目标表'的错误。

0

在我的情况下,这不是权限问题,而是表名问题中的特殊字符(括号和&)。

希望这会有帮助

0

Bulkcopy期望表存在于数据库中。你也应该有权访问这个数据库或表。

1

有趣的是,这也会发生,如果你有一个纯数字表名。用一个或多个字母字符开始表名,它工作得很好。

4

我的问题是有点不同的,原来我的表名是SQL保留关键字,所以我不得不做以下几点:

bulkCopy.DestinationTableName = $"{schema}.[{tableName}]"; 

哪里schema是目标模式和tableName目标表的名称

documentation

DestinationTableName is a three-part name [database].[owningschema].[name]. You can qualify the table name with its database and owning schema if you choose. However, if the table name uses an underscore ("_") or any other special characters, you must escape the name using surrounding brackets as in ([database].[owningschema].[name_01])

+0

是的,这也是我的问题。 从MSDN: DestinationTableName是一个三部分名称(。)。如果您选择,您可以使用数据库和拥有架构来限定表名。**但是,如果表名使用下划线(“_”)或任何其他特殊字符,则必须使用括号括起来(* [])使名称转义。 – bluedot 2017-09-14 19:19:25

+0

谢谢@bluedot我已经更新了我的答案和你的评论详情 – 2017-09-15 05:23:57