我的代码调用存储过程有什么问题

时间:2009-08-12 10:28:25

标签: c# sql-server stored-procedures

我创建了一个存储过程dbo.test,如下所示

    use sample 
    go
    create procedure dbo.test as

    DECLARE @command as varchar(1000), @i int
    SET @i = 0
    WHILE @i < 5
    BEGIN
      Print 'I VALUE ' +CONVERT(varchar(20),@i)
    SET @i = @i + 1
    END

现在我创建了一个c#控制台应用程序来调用存储过程,如下所示

   using System;
   using System.Collections.Generic;
   using System.Text;
   using System.Data;
   using System.Data.SqlClient;
   namespace AutomationApp
   {
     class Program
     {
       public void RunStoredProc()
   {
    SqlConnection conn = null;
    SqlDataReader rdr  = null;



    try
    {

  conn = new SqlConnection("Server=(TEST\\SQL2K5EXPRESS);DataBase=sample,IntegratedSecurity=SSPI");
  conn.Open();
  SqlCommand cmd  = new SqlCommand("sample.dbo.test", conn);
  cmd.CommandType = CommandType.StoredProcedure;
        //cmd.ExecuteNonQuery();
  rdr = cmd.ExecuteReader();
  while (rdr.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
              rdr[0], rdr[1]));
        }
       }
           catch(Exception ex)
        {
          Console.writeLine(ex.message);
         }      
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
        if (rdr != null)
        {
            rdr.Close();
        }
    }
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");

        Program p= new Program();
        p.RunStoredProc();      
        Console.Read();
    }

}

}

   O/P:
   Hello World
   //Could not find stored procedure 'test'.
   A network-related or instance-specific error occurred while establishing a conne

到SQL Server。服务器未找到或无法访问。验证  实例名称正确,并且SQL Server配置为允许远程 连接。 (提供者:SQL网络接口,错误:26 - 错误定位服务 r /指定的实例)

但是当我尝试运行该程序时它正在关闭所以我调试了程序然后在executeReader()方法中,它显示无法找到存储过程“dbo.test” 当我在SSMS上给出“EXEC dbo.test”时,它会显示我预期的结果。

对此任何帮助都非常赞赏。

7 个答案:

答案 0 :(得分:2)

为什么要查看master数据库?

此外,您的代码需要进行一些清理:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace AutomationApp
{
    class Program
    {
        public void RunStoredProc()
        {
            Console.WriteLine("\nTop 10 Most Expensive Products:\n");

            using (SqlConnection conn = 
                new SqlConnection(
                    "Server=(local);DataBase=master;IntegratedSecurity=SSPI"))
            {
                conn.Open();
                using (SqlCommand cmd = 
                      new SqlCommand("dbo.test", conn) {
                          CommandType = CommandType.StoredProcedure})
                {
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            Console.WriteLine(String.Format("{0}, {1}",
                                                            rdr[0], rdr[1]));
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

我建议它与授予存储过程的权限相关。 查看“GRANT EXECUTE&lt; storedProc&gt; TO&lt; User&gt;”您还可以通过右键单击存储过程来检查SSMS中的权限。

我知道这是一个测试存储过程,但在master数据库中创建存储过程并不是最佳的。

一切顺利:)

答案 2 :(得分:1)

只需更改连接字符串:

conn = new SqlConnection("Server=(local);DataBase=master;IntegratedSecurity=SSPI");

您正在访问master数据库。你想要点击你的数据库。

或者,将存储过程称为dbname.dbo.test

答案 3 :(得分:1)

首先 - 你的连接字符串中有拼写错误 - 你需要使用所有分号 - 不是分号一次而是逗号另一次。在DAtaBase =和IntegratedSEcurity之间,你有一个逗号 - 加上它必须是“集成安全性”(带空格!)。有关如何正确创建连接字符串的详细信息,请访问www.connectionstrings.com。

您帖子中的原始连接字符串:

conn = new SqlConnection(
  "Server=(TEST\\SQL2K5EXPRESS);DataBase=sample,IntegratedSecurity=SSPI");

应该是:

 conn = new SqlConnection(
   "Server=(TEST\\SQL2K5EXPRESS);DataBase=sample;Integrated Security=SSPI");

一旦这样可行,我认为主要的问题是你试图将参数@command中的SQL语句传递给存储过程,该存储过程在存储过程中执行(而不是在当前存储过程中)发布,但在你发布的其他内容中,并且你想要读出该语句返回的行。

但是你无法确定(没有解析你传入的SQL语句)是否该SQL语句实际上确实会返回值,或者它是否是一个DDL语句,如INSERT,CREATE TABLE或其他东西。 / p>

所以你有ADO.NET,调用存储过程,动态执行任意SQL语句,你仍然希望能够检索这些结果....

在我看来,你需要重新构建你的方法 - 这将永远无法可靠地工作 - 找到另一种方法来做你正在尝试做的事情 - 必须有一种方法来做到这一点,在存储中的SQL动态执行较少proc和东西........必须有一个更简单的方法!

马克

答案 4 :(得分:0)

您是否尝试过使用“test”而不是“dbo.test”?

答案 5 :(得分:0)

您的存储过程不会返回任何结果。将PRINT更改为SELECT。 您可能会收到错误,因为ADO.NET不知道如何对print语句导致的状态消息做出反应。

答案 6 :(得分:0)

您看到的SQL错误是连接到服务器。它甚至没有那么远,所以它根本与权限无关。对于您的客户端,如果您连接到“Server = GroverCleveland \ Instance1”(假设您的网络上不存在),则会收到相同的错误。

我认为问题在于您将服务器名称包装在parens中,如果您可以使用同一客户端盒上的其他SQL客户端连接到它。请尝试以下方法:

conn = new SqlConnection("Server=TEST\\SQL2K5EXPRESS;DataBase=sample;IntegratedSecurity=SSPI");
马克对分号是正确的,顺便说一下。查看connectionstrings.com了解详细信息...