【问题标题】:Visual Studio Server Explorer connects to database but in code an exception is thrownVisual Studio Server Explorer 连接到数据库,但在代码中引发异常
【发布时间】:2012-02-25 20:14:00
【问题描述】:

我有一个非常特殊的问题。在 VS2010 Server Explorer 中,我可以很好地连接到 SQL Server 并执行存储过程。但是,当我尝试在代码中执行此操作时,会引发异常:

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>

同样,服务器资源管理器可以连接到服务器并执行存储过程,但我的代码不断抛出相同的异常。这可能是什么原因造成的?

谢谢

编辑
明确一点:服务器资源管理器可以使用我的代码使用的相同连接字符串连接和执行存储过程。然而我的代码抛出了一个异常。

【问题讨论】:

    标签: c# sql-server sql-server-2008 stored-procedures server-explorer


    【解决方案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 域用户创建代理凭据 SHIPPING\KobeR 具有 Windows 密码 sdfh%dkc93vcMt0。

    【讨论】:

      【解决方案2】:

      您能否调试并确认来自 web.config 的连接字符串实际上正在加载到 connString 变量中?

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

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

      【讨论】:

      • \hosting 是一个命名实例。如果 SQL Server 浏览器正在运行,则不需要指定端口;我一直能够在不指定端口的情况下连接到命名实例。
      猜你喜欢
      • 2014-03-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多