0

我有一个非常奇怪的问题。在VS2010服务器资源管理器中,我可以连接到SQL Server并执行存储过程。然而,当我尝试这样做的代码我得到一个异常抛出:Visual Studio服务器资源管理器连接到数据库,但在代码中抛出异常

xp_cmdshell的代理帐户信息无法检索或 无效。验证“## xp_cmdshell_proxy_account ##”凭证 存在并且包含有效信息。

现在,也许我在代码中使用了错误的凭据,但后来我从服务器资源管理器复制了连接字符串,并将其放入我的配置文件中的连接字符串中。不过,同样的错误。

这里是做存储过程的连接和调用代码:

public static DataSet callStoredProcedure(string procedure, params SqlParameter[] args) 
{ 
    SqlDataAdapter adapter = new SqlDataAdapter(); 
    DataSet dataSet = new DataSet(); 

    string connString = ConfigurationManager.ConnectionStrings["App"].ConnectionString; 

    try 
    { 
     using (SqlConnection conn = new SqlConnection(connString)) 
     using (SqlCommand command = conn.CreateCommand()) 
     { 
      conn.Open(); 
      if (args != null) 
      { 
       foreach (SqlParameter param in args) command.Parameters.Add(param); 
      } 
      command.CommandText = procedure; 
      command.CommandType = CommandType.StoredProcedure; 

      adapter.SelectCommand = command; 
      adapter.Fill(dataSet, "tabela"); 
     } 
    } 
    catch (SqlException exception) 
    { 
     throw new Exception("SqlClient exception", exception); 
    } 

    return dataSet; 
} 

这里是连接字符串,它是从服务器资源管理器使用连接字符串复制相关的XML配置:

<connectionStrings> 
    <add name="App" connectionString="Data Source=db2.myapp.com,49178\hosting;Initial Catalog=app;User ID=appuser;Password=f00barbaz" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

此外,服务器资源管理器可以连接到服务器并执行存储过程,但我的代码不断抛出相同的异常。什么可能导致这个?

谢谢

编辑
要清楚:服务器资源管理器可以都连接,并与我的代码使用相同的连接字符串执行存储过程。然而我的代码抛出一个异常。

回答

1

您的存储过程调用xp_cmdshell?

如果是这样,请参阅:

http://msdn.microsoft.com/en-us/library/ms175046.aspx

当由不固定 服务器角色的系统管理员的成员的用户调用xp_cmdshell的使用帐户名 连接到Windows以及存储在名为 xp_cmdshell_proxy_account的凭证中的密码。如果此代理证书不存在,则xp_cmdshell将失败。

通过执行 sp_xp_cmdshell_proxy_account可以创建代理帐户凭证。作为参数,此存储过程 需要Windows用户名和密码。例如,以下 命令将为Windows域用户 创建代理凭据,该密码具有Windows密码sdfh%dkc93vcMt0的SHIPPING \ KobeR。

0

你能调试并确认来自web.config的conenction字符串实际上是beeing加载到connString变量吗?

也这个Data Source=db2.myapp.com,49178\hosting看起来很奇怪。我看到服务器,端口,但托管什么?

Initial Catalog=app is app the name of your DB? 
+1

'\ hosting'是一个命名实例。如果SQL Server浏览器正在运行,则不需要端口规范;我一直能够连接到命名实例而不指定端口。 – 2012-02-25 20:27:12

相关问题