为每个循环传递城市ID,但只有最后一个城市ID [英] Passing city id using for each loop but only last city id takes

查看:69
本文介绍了为每个循环传递城市ID,但只有最后一个城市ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Getfunction(int j)

{



string connectionString = ConfigurationManager.ConnectionStrings [OneTransportConnectionString]。ConnectionString;

SqlConnection sqlConnection = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand();

SqlDataReader reader;



cmd.CommandText =选择DISTINCT date_TripDate AS TripDate,traveltype.varchar_TravelTypeCode AS TripType,contactss.Value作为Mobilenumber,time_TripTime AS ShiftTime,trip.int_VehicleTripID作为RequestID,varchar_TravelerID作为AssociateID,(Associate_FirstName + Associate_LastName )AS AssociateName,Gender AS Gender,varchar_ProjectId AS ProjectID,varchar_ProjectName AS ProjectName,city.varchar_CityName AS City,placeee.varchar_PlaceName AS SourceArea,place.varchar_PlaceName AS DestinationArea,varchar_StatusDescription As Status,reason.varchar_ReasonDescription AS AppType



cmd.Com mandText + =来自[OneC_988]。[dbo]。[988_Details_VehicleRequest]请求;

cmd.CommandText + =加入[OneC_988]。[dbo]。[988_Details_VehicleTrip] trip on request.int_VehicleRequestID = trip.int_VehicleRequestID;



cmd.CommandText + =and int_CityID = @par;

cmd.Parameters.Add( @par,SqlDbType.Int).Value = j;



cmd.CommandType = CommandType.Text;

cmd.Connection = sqlConnection ;

sqlConnection.Open();

reader = cmd.ExecuteReader();

sqlConnection.Close();



}







static void Main(string [ ] args)

{

程序ps =新程序();

int [] numbers = {1,2,4,6, 8,11};

foreach(数字中的int j)

{

ps.Getfunction(j);

System.Console.Write({0},j);

}

}



i为每个传递上述城市ID,然后传入Getfunction。



但是在下面只有它只迭代J中的最后一个值11
cmd.Parameters.Add(@ par,SqlDbType.Int).Value = j; (11))



我的上述代码有什么错误



我有什么尝试过:



为每个循环使用城市ID,但只有最后一个城市ID需要。



i为每个传递上面的城市id,然后传递给Getfunction。



但是在下面它只迭代J中的最后一个值11 />
cmd.Parameters.Add(@ par,SqlDbType.Int).Value = j; (11))



上面代码中的错误是什么

public void Getfunction(int j)
{

string connectionString = ConfigurationManager.ConnectionStrings["OneTransportConnectionString"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "select DISTINCT date_TripDate AS TripDate,traveltype.varchar_TravelTypeCode AS TripType,contactss.Value as Mobilenumber,time_TripTime AS ShiftTime,trip.int_VehicleTripID as RequestID,varchar_TravelerID AS AssociateID,(Associate_FirstName + Associate_LastName) AS AssociateName,Gender AS Gender,varchar_ProjectId AS ProjectID,varchar_ProjectName AS ProjectName,city.varchar_CityName AS City,placeee.varchar_PlaceName AS SourceArea,place.varchar_PlaceName AS DestinationArea,varchar_StatusDescription As Status,reason.varchar_ReasonDescription AS AppType"

cmd.CommandText += " from [OneC_988].[dbo].[988_Details_VehicleRequest] request";
cmd.CommandText += " join [OneC_988].[dbo].[988_Details_VehicleTrip] trip on request.int_VehicleRequestID=trip.int_VehicleRequestID";

cmd.CommandText += "and int_CityID = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;

cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();

}



static void Main(string[] args)
{
Program ps = new Program();
int[] numbers = { 1, 2, 4, 6, 8, 11 };
foreach (int j in numbers)
{
ps.Getfunction(j);
System.Console.Write("{0} ", j);
}
}

i pass the above city id using for each, then pass into the Getfunction.

but in the below only it iterates only the last value 11 in the J
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j; (11 is)

what is the mistake in my above code

What I have tried:

passing city id using for each loop but only last city id takes.

i pass the above city id using for each, then pass into the Getfunction.

but in the below only it iterates only the last value 11 in the J
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j; (11 is)

what is the mistake in my above code

推荐答案

你应该学习使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于为每个循环传递城市ID,但只有最后一个城市ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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