使用用户名密码组合时找不到存储过程错误
Could not find stored procedure error when using username password combination
我正在尝试从我的 C# 代码调用存储过程并使用用户定义的 table 类型传递 table 参数。
当我在连接字符串中使用 Trusted_Connection= True;
方法时,这工作得很好。但是,当我更新连接字符串以使用服务帐户用户名密码组合时,出现以下错误:
Could not find stored procedure 'usp_MyTestSP'
顺便说一句,这是我用来调用该存储过程并传递 table 参数的代码:
// Works fine with this connection string
var conString = "Data Source=xxx;Initial Catalog=xxx;Trusted_Connection=True;";
// Fails when using a service account in connection string. Error: Could not find stored procedure 'usp_MyTestSP'
//var conString = "Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx";
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("usp_MyTestSP", con) {CommandType = CommandType.StoredProcedure};
var myParams = new SqlParameter()
{
ParameterName = "@udt_RecordProcessed",
Value = myVal
};
cmd.Parameters.Add(myParams);
con.Open();
cmd.ExecuteNonQuery();
}
真的是权限问题吗?
当我使用自定义服务帐户连接到 SQL 服务器实例时,我实际上能够 查看 代码抱怨不存在的存储过程能够找到。
我的服务帐户是否需要一些额外的权限才能执行该存储过程?
或者...我的代码中是否需要更新某些内容才能与服务帐户一起使用?虽然相同的代码在使用受信任的连接时工作得很好。
此处的任何输入都会非常有帮助。
谢谢。
我认为您的代码有问题,连接字符串设置为 SqlConnection
实例,您必须使用 conString
变量而不是 cs
变量:
using (var con = new SqlConnection(conString))
我能够通过将 [db_owner]
前缀添加到存储过程名称 usp_MyTestSP
来实现此功能,如以下代码所示:
// Works fine with this connection string
var conString = "Data Source=xxx;Initial Catalog=xxx;Trusted_Connection=True;";
// Fails when using a service account in connection string. Error: Could not find stored procedure 'usp_MyTestSP'
//var conString = "Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx";
using (var con = new SqlConnection(conString))
{
// ** The fix was to prefix the stored procedure name with [db_owner]
var cmd = new SqlCommand("[db_owner].[usp_MyTestSP]", con) {CommandType = CommandType.StoredProcedure};
var myParams = new SqlParameter()
{
ParameterName = "@udt_RecordProcessed",
Value = myVal
};
cmd.Parameters.Add(myParams);
con.Open();
cmd.ExecuteNonQuery();
}
我正在尝试从我的 C# 代码调用存储过程并使用用户定义的 table 类型传递 table 参数。
当我在连接字符串中使用 Trusted_Connection= True;
方法时,这工作得很好。但是,当我更新连接字符串以使用服务帐户用户名密码组合时,出现以下错误:
Could not find stored procedure 'usp_MyTestSP'
顺便说一句,这是我用来调用该存储过程并传递 table 参数的代码:
// Works fine with this connection string
var conString = "Data Source=xxx;Initial Catalog=xxx;Trusted_Connection=True;";
// Fails when using a service account in connection string. Error: Could not find stored procedure 'usp_MyTestSP'
//var conString = "Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx";
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("usp_MyTestSP", con) {CommandType = CommandType.StoredProcedure};
var myParams = new SqlParameter()
{
ParameterName = "@udt_RecordProcessed",
Value = myVal
};
cmd.Parameters.Add(myParams);
con.Open();
cmd.ExecuteNonQuery();
}
真的是权限问题吗?
当我使用自定义服务帐户连接到 SQL 服务器实例时,我实际上能够 查看 代码抱怨不存在的存储过程能够找到。
我的服务帐户是否需要一些额外的权限才能执行该存储过程?
或者...我的代码中是否需要更新某些内容才能与服务帐户一起使用?虽然相同的代码在使用受信任的连接时工作得很好。
此处的任何输入都会非常有帮助。
谢谢。
我认为您的代码有问题,连接字符串设置为 SqlConnection
实例,您必须使用 conString
变量而不是 cs
变量:
using (var con = new SqlConnection(conString))
我能够通过将 [db_owner]
前缀添加到存储过程名称 usp_MyTestSP
来实现此功能,如以下代码所示:
// Works fine with this connection string
var conString = "Data Source=xxx;Initial Catalog=xxx;Trusted_Connection=True;";
// Fails when using a service account in connection string. Error: Could not find stored procedure 'usp_MyTestSP'
//var conString = "Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx";
using (var con = new SqlConnection(conString))
{
// ** The fix was to prefix the stored procedure name with [db_owner]
var cmd = new SqlCommand("[db_owner].[usp_MyTestSP]", con) {CommandType = CommandType.StoredProcedure};
var myParams = new SqlParameter()
{
ParameterName = "@udt_RecordProcessed",
Value = myVal
};
cmd.Parameters.Add(myParams);
con.Open();
cmd.ExecuteNonQuery();
}