c# - 从 C# 调用存储过程时出错

标签 c# sql sql-server-2005 stored-procedures ado.net

我有以下 c# 代码来调用存储过程 testproc,但是当我运行这个应用程序时,它说找不到存储过程 testproc。

这是我调用存储过程的c#代码:

SqlConnection con = new SqlConnection();
con.ConnectionString = "data source='example.com';user id='sa';password='password';persist security info=False;initial catalog=Test;Connect Timeout=100; Min Pool Size=100; Max Pool Size=500";
con.Open();

DataSet ds = new DataSet();
SqlCommand com = new SqlCommand("testproc",con );
SqlDataAdapter sqlda = new SqlDataAdapter(com);
//sqlda.SelectCommand.CommandText = "SELECT Id,Name1,ZipCode,StreetName,StreetNumber,State1,Lat,Lng,Keyword, ( 6371 * ACOS( COS( (12.925432/57.2958) ) * COS(  (Lat/57.2958)  ) * COS( ( Lng/57.2958 ) - (77.5940171/57.2958) ) + SIN( 12.925432/57.2958 ) * SIN(  Lat/57.2958  ) ) ) AS distance FROM Business_Details where( (StreetName like '%jayanagar%')and (Keyword like '%plumbing%' ))ORDER BY distance;";
//sqlda.CommandText = "select * from business where(( distance<'" + radius + "' )and (StreetName like '%" + streetname + "%')and (Keyword like '%" + keyword1 + "%' )) order by distance";
//com.CommandText = "testproc ";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@lat1",SqlDbType.Float,50,lat1));
com.Parameters.Add(new SqlParameter("@lng1",SqlDbType.Float,50,lng1));
com.Parameters.Add(new SqlParameter("@radius1",SqlDbType.Int,10,radius1));
com.Parameters.Add(new SqlParameter("@streetname", SqlDbType.VarChar, 50, streetname));
com.Parameters.Add(new SqlParameter("@keyword1", SqlDbType.VarChar, 50, keyword1)); 
com.Parameters[0].Value = lat1;
com.Parameters[1].Value = lng1;
com.Parameters[2].Value = radius1;
com.Parameters[3].Value = streetname;
com.Parameters[4].Value = keyword1;
try
{                
    sqlda.Fill(ds);
    con.Close();
}
catch (Exception e)
{
    con.Close();
}

这是我用sql server写的存储过程。在SQL Server中运行成功

USE [Test]
GO
/****** Object:  StoredProcedure [dbo].[tesproc]    Script Date: 09/01/2010 13:00:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 ALTER PROCEDURE [dbo].[tesproc]
    -- Add the parameters for the stored procedure here
    @a float, @b float, @d int,@s varchar(50),@k varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select Id, Name1,ZipCode,StreetName,StreetNumber,State1,Lat,Lng , ( 6371 * ACOS( COS( (@a/57.2958) ) * COS(  (Lat/57.2958)  ) * COS( ( Lng/57.2958 ) - (@b /57.2958) )  + SIN( @a/57.2958 ) * SIN(  Lat/57.2958  ) ) ) as distance from business_details where (( 6371 * ACOS( COS( (@a/57.2958) ) * COS(  (Lat/57.2958)  ) * COS( ( Lng/57.2958 ) - (@b /57.2958) )  + SIN( @a/57.2958 ) * SIN(  Lat/57.2958  ) ) )<@d and StreetName like '%'+ @s + '%' and Keyword like '%'+ @k +'%')
END

最佳答案

您的 ALTER 语句将其称为 tesproc,但您的 SQL 命令将其称为 testproc

关于c# - 从 C# 调用存储过程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3617533/

相关文章:

Mysql错误: #1075

sql - 使用 exec() 在 sp 内执行运行时查询时遇到错误 "The default schema does not exist."

c# - Linq to 对象和 lambda 表达式

MySQL 查询耗时较长。我尝试了多种查询变体,但似乎没有任何影响速度

c# - 非特例,数组越界

mysql - Mysql中Postgresql regexp_split_to_table有什么解决办法

database - 如何连接来自两个不同数据库的两个表?

sql - 如何从另一个存储过程调用一个存储过程?

c# - .css 文件中的 html 样式链接?

c# - Popup 中的 UI 虚拟化