sql server 2008中的存储过程 [英] stored procedures in sql server 2008

查看:86
本文介绍了sql server 2008中的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 DataSet ds = GetDataSet( 从车辆点选择VehicleId,Datetime,Location vehicleid'' + Sno +  '和(日期之间'' + txtstart。文字+  '和' + txtend.Text +   ')按VehicleId分组,日期时间,按日期时间的位置顺序); 

for int i = 0 ; i < ds.Tables [ 0 ]。Rows.Count - < span class =code-digit> 1 ; i ++)
{

DateTime dt1 = Convert.ToDateTime(ds.Tables [ 0 ]。行[i] [ 日期时间]);
int j = i + 1 ;
DateTime dt2 = Convert.ToDateTime(ds.Tables [ 0 ]。行[j] [ 日期时间]);
TimeSpan tspan =(dt2 - dt1);

int intMinutes = Convert.ToInt32(tspan.TotalMinutes);
string vid = ds.Tables [ 0 ]。行[i] [ VehicleId]。ToString();
string loc = ds.Tables [ 0 ]。行[i] [ 位置]。ToString();

RunQueryWOCheck( 插入IdleReport(VehicleNo,Startdate,Enddate,Duration,Location)值(' + vid + ',' + dt1 + ',' + dt2 + ',' + intMinutes + ',' + loc + '));

}



这是编码c#如何使用sql server 2008转换存储过程

解决方案

如果您不知道如何创建存储过程,请检查以下链接

http://technet.microsoft.com/en-us/library/aa174792%28v=sql.80%29.aspx [ ^ ]



因为如果您可以编写Store程序,那么您可以根据自己的意愿创建。



这是一个您的代码示例,请根据您的要求修改





   -    =============================== ==============  
- 作者: MUKESH GHOSH
- 创建日期:14-08-2013
- 描述:
- < span class =code-comment> =========================================== ====
ALTER PROCEDURE [dbo]。[stp_Save_Vehicle]
@ VehicleID INT
AS
BEGIN
DECLARE @Vehicle TABLE
VehicleId INT ,DatetimeV 日期时间,位置 NVARCHAR 100 ));

DECLARE @ VehicleFilter TABLE
VehicleId INT ,DatetimeV 日期时间,位置 NVARCHAR 100 ));

DECLARE @ IdleReport TABLE
(VehicleNo INT ,Startdate DATETIME ,Enddate DATETIME ,持续时间 NVARCHAR 50 ),位置 NVARCHAR 50 ))

DECLARE @ count INT @ rowCount < span class =code-keyword> INT , @ Location1 NVARCHAR 100
INSERT INTO @ Vehicle (VehicleId,DatetimeV,Location)
VALUES 1 ,GETDATE(),' DELHI'
INSERT INTO @ Vehicle (VehicleId,DatetimeV,Location)
VALUES 1 ,GETDATE(),' BOMBAY'
INSERT INTO @ Vehicle (VehicleId, DatetimeV,Location)
VALUES 2 ,GETDATE(),' CHENNI'

INSERT < span class =code-keyword> INTO @ VehicleFilter
S ELECT * FROM @ Vehicle V WHERE V.VehicleId=@VehicleID


SET @ count = 1
SELECT @ rowCount = COUNT( 1 FROM @ VehicleFilter

WHILE @ count < = @ rowCount
BEGIN
SELECT @ Location1 =位置 FROM @ VehicleFilter

INSERT INTO @ IdleReport (VehicleNo,Startdate,Enddate,Duration,Location) VALUES
1 ,GETDATE(),GETDATE(),DATEDIFF(MINUTE,GETDATE(),GETDATE()), @ Location1
SET @ count = @ count + 1
END
END





[edit]已添加代码块[/ edit]


这是一个很好的首发:



Sql Server - 如何在Sql server中编写存储过程 [ ^ ] <顺便提一句:


:你的常规代码(使用(... where column ='+ TextBox1.Text +')的代码很容易被sql注入。尝试使用存储过程,将数据库依赖项隔离到类中。


DataSet ds = GetDataSet("select VehicleId,Datetime,Location from vehiclepoints where vehicleid='" + Sno + "' and (Datetime between '" + txtstart.Text + "' and '" + txtend.Text + "') group by VehicleId,Datetime,Location order by Datetime");
 
for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
{

 DateTime dt1 = Convert.ToDateTime(ds.Tables[0].Rows[i]["Datetime"]);
 int j = i + 1;
 DateTime dt2 = Convert.ToDateTime(ds.Tables[0].Rows[j]["Datetime"]);
 TimeSpan tspan = (dt2 - dt1);

 int intMinutes = Convert.ToInt32(tspan.TotalMinutes);
 string vid = ds.Tables[0].Rows[i]["VehicleId"].ToString();
 string loc = ds.Tables[0].Rows[i]["Location"].ToString();
 
RunQueryWOCheck("insert into IdleReport(VehicleNo,Startdate,Enddate, Duration,Location)values('" + vid + "','" + dt1 + "','" + dt2 + "','" + intMinutes + "','" + loc + "')");

}   


this is coding for c# how to convert in stored procedure by using sql server 2008

解决方案

If you don't know how to create stored procedure then check for bellow link
http://technet.microsoft.com/en-us/library/aa174792%28v=sql.80%29.aspx[^]

Because if you can write Store procedure then later you can create as per your wish.

Here is a sample of your code, please modify as per your requirement


-- =============================================
-- Author:		MUKESH GHOSH
-- Create date: 14-08-2013
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[stp_Save_Vehicle]
	@VehicleID INT
AS
BEGIN
	DECLARE @Vehicle TABLE (
	VehicleId INT,DatetimeV Datetime,Location NVARCHAR(100));
	
	DECLARE @VehicleFilter TABLE (
	VehicleId INT,DatetimeV Datetime,Location NVARCHAR(100));
	
	DECLARE @IdleReport TABLE
	(VehicleNo INT,Startdate DATETIME,Enddate DATETIME,Duration NVARCHAR(50),Location NVARCHAR(50))
	
	DECLARE @count INT, @rowCount INT,@Location1 NVARCHAR(100) 
	INSERT INTO @Vehicle (VehicleId,DatetimeV,Location)
	VALUES(1,GETDATE(),'DELHI')
	INSERT INTO @Vehicle (VehicleId,DatetimeV,Location)
	VALUES(1,GETDATE(),'BOMBAY')
	INSERT INTO @Vehicle (VehicleId,DatetimeV,Location)
	VALUES(2,GETDATE(),'CHENNI')
	
	INSERT INTO @VehicleFilter
	SELECT * FROM @Vehicle V WHERE V.VehicleId=@VehicleID
	
	
	SET @count = 1
	SELECT @rowCount = COUNT(1) FROM @VehicleFilter
	
	WHILE @count <= @rowCount
	BEGIN
		SELECT @Location1=Location FROM @VehicleFilter
		
		INSERT INTO @IdleReport(VehicleNo,Startdate,Enddate, Duration,Location)VALUES
		(1,GETDATE(),GETDATE(),DATEDIFF(MINUTE,GETDATE(),GETDATE()),@Location1)
	SET @count = @count + 1
	END
END



[edit]Code block added[/edit]


this is a good starter:

Sql Server - How to write a Stored procedure in Sql server[^]

by the way: your conventional code (the one that uses (...where column='"+TextBox1.Text+"') is vulnerable to sql injection. Try to use stored procedure, segregate database dependents into a class.


这篇关于sql server 2008中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆