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

发布于 2024-09-16 12:02:28 字数 2887 浏览 6 评论 0原文

我有以下 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

I have the following c# code to call stored procedure testproc, but when I run this application it says that it could not find stored procedure testproc.

This is my c# code behind to call the stored procedure:

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();
}

This is my stored procedure I have written in sql server. It runs successfully in 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久夏青 2024-09-23 12:02:28

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

Your ALTER statement calls it tesproc, but your SQL command calls it testproc

夏雨凉 2024-09-23 12:02:28

检查拼写、调用您的过程

[dbo].[tesproc]

以及调用您的代码

SqlCommand("testproc",con );

您还可以使用

Parameters.AddWithValue(string parameterName, Object value)

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

如果您愿意:)

Check your spelling, your procedure is called

[dbo].[tesproc]

and your code is calling

SqlCommand("testproc",con );

You can also add parameters using

Parameters.AddWithValue(string parameterName, Object value)

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

if you wanted to :)

蓝礼 2024-09-23 12:02:28

不管怎样,你的代码应该是这样的:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlComamnd command = connection.CreateCommand())
{
    command.CommandText = commandText;
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add("@lat1", SqlDbType.Float,50, lat1).Value = lat1;
    command.Parameters.Add("@lng1", SqlDbType.Float,50, lng1).Value = lng1;
    command.Parameters.Add("@radius1", SqlDbType.Int,10, radius1).Value = radius1;
    command.Parameters.Add("@streetname", SqlDbType.VarChar, 50, streetname).Value = streetname;
    command.Parameters.Add("@keyword1", SqlDbType.VarChar, 50, keyword1).Value = keyword1;

    connection .Open();
    DataSet ds = new DataSet();
    using (SqlDataAdapter adapter = neq SqlDataAdapter(command))
    {
        adapter.Fill(ds);                
    }
}

Anyway your code should look like this:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlComamnd command = connection.CreateCommand())
{
    command.CommandText = commandText;
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add("@lat1", SqlDbType.Float,50, lat1).Value = lat1;
    command.Parameters.Add("@lng1", SqlDbType.Float,50, lng1).Value = lng1;
    command.Parameters.Add("@radius1", SqlDbType.Int,10, radius1).Value = radius1;
    command.Parameters.Add("@streetname", SqlDbType.VarChar, 50, streetname).Value = streetname;
    command.Parameters.Add("@keyword1", SqlDbType.VarChar, 50, keyword1).Value = keyword1;

    connection .Open();
    DataSet ds = new DataSet();
    using (SqlDataAdapter adapter = neq SqlDataAdapter(command))
    {
        adapter.Fill(ds);                
    }
}
驱逐舰岛风号 2024-09-23 12:02:28

而不是使用这个庞大的代码

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;

并且您再次为参数分配值

使用 AddWithValue 方法将参数添加到命令,

com.Parameters.AddWithValue("@lat1", lat1));
com.Parameters.AddWithValue("@lng1", lng1));
com.Parameters.AddWithValue("@radius1", radius1));
com.Parameters.AddWithValue("@streetname", streetname));
com.Parameters.AddWithValue("@keyword1", keyword1));

Instead of this bulky code

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;

And you are assigning value again to parameters

use AddWithValue method to add parameter to command

com.Parameters.AddWithValue("@lat1", lat1));
com.Parameters.AddWithValue("@lng1", lng1));
com.Parameters.AddWithValue("@radius1", radius1));
com.Parameters.AddWithValue("@streetname", streetname));
com.Parameters.AddWithValue("@keyword1", keyword1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文