2012-01-17 46 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