在Excel表(以transferTable)看完后,我想将数据添加到使用SqlBulkCopy的新表(destinationTable会),但我得到的错误:
Cannot access destination table 'test'
Run Code Online (Sandbox Code Playgroud)
我尝试使用默认的表名并使用方括号,但这不起作用.
有什么建议?
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(); …Run Code Online (Sandbox Code Playgroud) 无论我尝试什么,我都会收到这个错误.
我有一个存储过程,执行为:
CREATE PROCEDURE usp_myproc
WITH EXECUTE AS 'myuser'
Run Code Online (Sandbox Code Playgroud)
在这个sp我有
EXEC('INSERT INTO ' + @tablename + '
SELECT col1, col2, col3
FROM OPENROWSET(
BULK '''+ @filepath +''',
FORMATFILE='''+ @formatfile +''',
FIRSTROW=2
)as t'
)
Run Code Online (Sandbox Code Playgroud)
将myuser确实有bulkadmin角色,读/写,创建表,插入,选择,执行,修改权限.其中一些可能不需要,但这是我到目前为止所尝试的.我错过了什么?
谢谢.