如何为每个循环传递城市 [英] How to pass city using for each loop
本文介绍了如何为每个循环传递城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下
以下int_city id如下1,2,4,6,8,11
int [] numbers = { 1 , 2 , 4 , 6 ,< span class =code-digit> 8 , 11 };
foreach ( int i in 数字)
{
System.Console.Write( {0},i);
}
string connectionString = ConfigurationManager.ConnectionStrings [ < span class =code-string> OneTransportConnectionString]。ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader阅读器;
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为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 + = 来自[OneC_988]。[dbo]。[988_Details_VehicleRequest]请求
cmd.CommandText + = join [OneC_988]。[dbo]。[988_Details_VehicleTrip] trip on request.int_VehicleRequestID = trip.int_VehicleRequestID
cmd.CommandText + = 和int_CityID =
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();
int_cityid中的
我想通过以下内容
int [] numbers = { 1 , 2 , 4 , 6 , 8 , 11 };
foreach ( int i in 数字)
{
System.Console.Write(( {0},i);
}
上面的一个是通过多个城市的条件。
for asp.net如何使用c#
我尝试过:
如何为每个循环传递城市
解决方案
制作参数化查询 [ ^ ]并在 foreach
循环中调用它。
SELECT ... FROM ... WHERE int_CityID IN ( 1 , 2 , 4 , 6 , 8 , 11 )
您可以使用 string.Join(,,number)
生成数组中的ID值列表。
试试这个
cmd.CommandText + = 和int_CityID在 + ( + string .Join( ,,numbers)+ );
My code as follows
The below int_city id is as follows 1,2,4,6,8,11
int[] numbers = { 1, 2, 4, 6, 8, 11 };
foreach (int i in numbers)
{
System.Console.Write("{0} ", i);
}
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 = " "
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();
in int_cityid i want to pass the below thing
int[] numbers = { 1, 2, 4, 6, 8, 11 };
foreach (int i in numbers)
{
System.Console.Write(("{0} ", i);
}
the above one is pass multiple city in the and condition.
for that how to do in asp.net using c#
What I have tried:
how to pass city using for each loop
解决方案
Make a parameterized query[^] and call it in your foreach
loop.
I think you mean:
SELECT ... FROM ... WHERE int_CityID IN (1, 2, 4, 6, 8, 11)
You can usestring.Join(",", numbers)
to generate the list of ID values from your array.
try this
cmd.CommandText += "and int_CityID in " + "(" + string.Join(",", numbers) + ")";
refer this regarding sql injection[^]
这篇关于如何为每个循环传递城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文