如何使用c#代码隐藏中的参数调用存储过程
我已经创建了一个如下所示的存储过程,我将如何从c#code behind调用它来获得结果,并将结果存储在dataset中如何使用c#代码隐藏中的参数调用存储过程,c#,asp.net,sql-server,sql-server-2005,C#,Asp.net,Sql Server,Sql Server 2005,我已经创建了一个如下所示的存储过程,我将如何从c#code behind调用它来获得结果,并将结果存储在dataset中 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]
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
查看和。查看和
互联网上有很多综合样本:
我的代码示例只是演示它可能的样子——它是直接在编辑器中编写的,因此可能无法单独工作
互联网上有很多综合样本:
我的代码示例只是演示它可能的样子——它是直接在编辑器中编写的,因此可能无法单独工作。您看到了吗?简单的Google搜索“从ADO.net调用存储过程”将为您提供所需的所有信息!!你看到了吗?简单的Google搜索“从ADO.net调用存储过程”将为您提供所需的所有信息!!
exec dbo.tesproc 12.9216667 ,57.2958,77.591667,1
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
}
}
}