如何调用存储过程与参数从c#代码

问题描述:

我已经创建了如下所示的存储过程,我将如何从后面的c#代码调用此函数来获取结果并将结果存储在数据集中。如何调用存储过程与参数从c#代码

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, @c float,@d int 
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/@b)) * COS( (Lat/@b) ) * COS((Lng/@b) - (@c/@b)) + SIN(@a/@b) * SIN( Lat/@b ))) as distance from business_details where (6371 * ACOS(COS((@a/@b)) * COS( (Lat/@b) ) * COS((Lng/@b) - (@c/@b)) + SIN(@a/@b) * SIN( Lat/@b )))<@d 
END 

如果我在SQL Server中执行此存储过程的做工精细与此以下调用

exec dbo.tesproc 12.9216667 ,57.2958,77.591667,1 
+1

你见过http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx吗?简单的Google搜索“从ADO.net调用存储过程”将为您提供所有您需要的信息! – InSane 2010-09-01 08:40:16

using (SqlConnection conn = new SqlConnection("connection string goes here")) 
using (SqlCommand comm = new SqlCommand("tesproc", conn)) 
{ 
    comm.CommandType = CommandType.StoredProcedure; 
    comm.Parameters.AddWithValue("@a", 0.1); 
    // etc 

    conn.Open(); 

    using (SqlDataReader reader = comm.ExecuteReader()) 
    { 
     while (reader.Read()) 
     { 
      int id = reader.GetInt32(reader.GetOrdinal("id")); 
      // etc 
     } 
    } 
} 

有很多在互联网上的综合样本:

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx

我的代码示例只是表明它可能是什么样子 - 这是写直在编辑器中,可能无法正常工作的它自己的。

+0

谢谢你的回复 – mahesh 2010-09-01 09:03:47

查找到SqlConnectionSqlCommand