我创建了一个存储过程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大受赞赏。
@command是否为空? – VVS 2009-08-12 10:36:27
当您在另一个数据库中创建过程并更改连接字符串以连接到该数据库并执行该过程时会发生什么? – Kirtan 2009-08-12 10:36:47
另请检查已创建过程的数据库服务器实例。它是真的(本地)还是(本地\ SQLExpress)或其他什么? – Kirtan 2009-08-12 10:38:32