批量插入问题

发布于 12-04 10:06 字数 1406 浏览 8 评论 0原文

我有一个存储过程,可以从某个文件中进行批量插入:

CREATE PROCEDURE [dbo].[SP_BulkInsert] @FileName NVARCHAR(200) AS
BEGIN
DECLARE @bulkinsert NVARCHAR(1000)

SET @bulkinsert = N'BULK INSERT TblTemp FROM ''' + @FileName + 
       N''' WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')'

EXEC sp_executesql @bulkinsert

RETURN @@ROWCOUNT
END

当我从 SQL Server Management Studio 运行该存储过程时,该存储过程运行良好,但是当我尝试使用 ADO.NET 的 ExecuteNonQuery 运行它时,我得到了出现以下错误:

“数据库对象“TblTemp”的 INSERT 权限被拒绝 “TempDB”,架构“dbo”。”

重要提示:所有其他存储过程(使 SELECT/INSERT/DELETE/UPDATE)都可以从 ADO.NET 正常运行。

运行所有内容的用户是 bulkadmin 角色的成员,也是自定义 db_executer 角色的成员(仅具有 EXECUTE 权限)。

该代码对于许多存储过程都运行良好,这是第一次失败.. 功能

public static int BulkInsert(string fileName)
{
    SqlParameter paramFileName = new SqlParameter("FileName", fileName);
    SqlParameter paramRetValue = new SqlParameter();
    paramRetValue.Direction = ParameterDirection.ReturnValue;
    SqlParameter[] @parameters = { paramFileName, paramRetValue };
    SqlHelper.ExecuteNonQuery(ConnectionSettings.ConnectionString,
    CommandType.StoredProcedure, "SP_BulkInsert", parameters, true);

    return (int)paramRetValue.Value;
}

这是我使用 ADO.NET 端连接字符串中相同的用户名/密码登录 SSMS 的

。最根本的问题是,为什么在 Management Studio 中存储过程成功,而通过 ADO.NET 却失败(并显示上述错误消息)。

I have a stored procedure that makes bulk insert from some file:

CREATE PROCEDURE [dbo].[SP_BulkInsert] @FileName NVARCHAR(200) AS
BEGIN
DECLARE @bulkinsert NVARCHAR(1000)

SET @bulkinsert = N'BULK INSERT TblTemp FROM ''' + @FileName + 
       N''' WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')'

EXEC sp_executesql @bulkinsert

RETURN @@ROWCOUNT
END

This stored procedure runs fine when I run it from SQL Server Management Studio, but when I try to run it with ExecuteNonQuery of ADO.NET I get the following error:

"The INSERT permission was denied on the object 'TblTemp', database
'TempDB', schema 'dbo'."

Important: all other stored procedures (that make SELECT/INSERT/DELETE/UPDATE) run fine from ADO.NET.

The user under which all the things are run is a member of the bulkadmin role, and also a member of a custom db_executer role (that has just EXECUTE permission).

The code runs fine for a lot of stored procedures, it's a first time that it fails..
This is the function

public static int BulkInsert(string fileName)
{
    SqlParameter paramFileName = new SqlParameter("FileName", fileName);
    SqlParameter paramRetValue = new SqlParameter();
    paramRetValue.Direction = ParameterDirection.ReturnValue;
    SqlParameter[] @parameters = { paramFileName, paramRetValue };
    SqlHelper.ExecuteNonQuery(ConnectionSettings.ConnectionString,
    CommandType.StoredProcedure, "SP_BulkInsert", parameters, true);

    return (int)paramRetValue.Value;
}

I logging into the SSMS with the same username/password that are in the connection string on ADO.NET side..

The bottom question is, why in Management Studio the stored procedure succeed, while via ADO.NET it fails (with the above error message).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城2024-12-11 10:06:21

通过 sp_executesql 运行 SQL 使用的权限与直接在存储过程中运行的权限不同。我建议检查您运行存储过程的用户是否具有(在本例中)针对表“TblTemp”的 INSERT 权限。

要在 Sql Server Management Studio 中执行此操作...

  • 展开表列表,
  • 右键单击相应的表,然后
  • 在“权限”选项卡上选择属性,然后单击“添加...”按钮。
  • 输入用户或角色,或者“浏览...”。
  • 在顶部表格中选择用户或角色后,在“{用户名/角色}的显式权限”中的相应权限中勾选“授予”,然后
  • 单击“确定”

Running SQL via the sp_executesql uses different permissions than directly in the stored procedure. I would advise checking that the user which you are running the stored procedure as has (in this instance) INSERT permissions against the table "TblTemp".

To do this in Sql Server Management Studio...

  • expand the list of tables
  • right click on the appropriate one and select properties
  • on the "permissions" tab, click the "Add..." button.
  • Either type in the user or role, or "Browse..." for it.
  • with the user or role selected in the top table, tick "Grant" in the appropriate permissions in the "Explicit permissions for {username/role}"
  • click OK
甲如呢乙后呢2024-12-11 10:06:21

Management Studio 必须在具有所有权限的 sa 帐户上运行,但通过 ADO.NET 连接的用户可能没有正确的权限集。您需要通过 Management studio 授予用户写入权限。查看用户列表。

The Management Studio must be running on the sa account which has all privileges, but the user that you are connecting through ADO.NET may not have the correct privileges set. You need to give the users writing permissions through the Management studio. Look in the users list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文