从代码运行SQL Server USP时出错

时间:2015-12-16 19:22:41

标签: c# asp.net sql-server webforms

我一直在尝试从我的C#代码运行SQL Server查询,但每次出现错误

  

dbo.123不存在

如果我登录SSMS并输入查询窗口exec dbo.123,则会运行该过程。为什么我的代码无法看到它?我正在连接到正确的服务器和数据库。

public DataSet RunSQLStoredProc()
{
    ebdb = new DataSet(); 
    SqlQueryBuilder = new StringBuilder();
    SqlQueryBuilder.Append("exec dbo.123 ");
    ebdb = DoThis(SqlQueryBuilder.ToString());
    return ebdb;
}

public DataSet DoThis(string sqlQuery)
{
    try
    {
        System.Configuration.ConnectionStringSettings connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer1"];

        using (SqlConnection conn = new SqlConnection(connstring.ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = sqlQuery;
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;

                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);                    
                adapter.Fill(ebdb);
                conn.Close();
            }                
        }

        return ebdb;
    }
    catch (Exception exception) { throw exception; }
}

1 个答案:

答案 0 :(得分:2)

Uri更改声明 到SqlQueryBuilder.Append("exec dbo.123 "); 123之后还有一个空格可能会产生问题,所以也要移除那个空间。