2009-08-12 70 views
0

我创建了一个存储过程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 

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

但是,当我尝试运行,将关闭以便程序我调试的程序,然后在正好在的ExecuteReader()方法,它是显示无法找到存储过程“dbo.test” 当我在SSMS上给出“EXEC dbo.test”时,它显示结果如我所料。

与这个任何帮助whngt大受赞赏。

+0

@command是否为空? – VVS 2009-08-12 10:36:27

+0

当您在另一个数据库中创建过程并更改连接字符串以连接到该数据库并执行该过程时会发生什么? – Kirtan 2009-08-12 10:36:47

+1

另请检查已创建过程的数据库服务器实例。它是真的(本地)还是(本地\ SQLExpress)或其他什么? – Kirtan 2009-08-12 10:38:32

回答

0

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

+0

是的,它也是同样的问题。 – Cute 2009-08-12 10:34:03

+0

看看罗素的回答。 – 2009-08-12 10:35:59

1

我会建议它与授予存储过程的权限相关。 查看“授予执行<storedProc>到<用户>”您也可以通过右键单击存储的过程来检查SSMS中的权限。

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

一切顺利:)

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

只要改变你的连接字符串:

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

你打的master数据库。你想打任何你的数据库被调用。

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

0

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

+0

ADO.NET只是执行存储过程,我有Supplied.in存储过程我只是打印我的价值....我想这里ut是连接问题第一个连接不建立到sqlserver – Cute 2009-08-12 12:13:38

+0

如果你只是执行存储proc并不返回任何内容,请使用** .executeNonQuery()**而不是ExecuteReader()! – 2009-08-12 14:04:42

+0

连接已打开。如果连接失败,连接失败。打开ExecuteReader()。 你有没有试过我的建议? – pipTheGeek 2009-08-12 20:42:14

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语句实际上是否确实会返回值,或者它是否是像INSERT,CREATE TABLE之类的DDL语句。

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

在我看来,你需要重新构建你的方法 - 这将无法可靠地工作 - 找到另一种方法来做你正在做的事情 - 必须有一种方法来执行此操作,在存储过程中执行SQL的动态执行较少,而且......有一个更简单的方法!

马克

+0

这指出了问题,答案和周围的材料。做得好。 – Beska 2009-08-12 21:22:00

0

您所看到的SQL错误连接到服务器。它甚至没有那么远,所以它根本与权限无关。就您的客户而言,如果连接到“Server = GroverCleveland \ Instance1”(假设您的网络中不存在),则会出现同样的错误。

我认为问题在于你正在将服务器名称包装在parens中,如果你可以在同一个客户端上使用其他SQL客户端进行连接。尝试以下操作:

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

马克是正确的分号,顺便说一句。请查看connectionstrings.com以了解有关详细信息...