如何在C#中以循环方式获取两个变量值?
我想在数据库中插入每个循环的两个变量。怎么做如何在C#中以循环方式获取两个变量值?,c#,sql-server,C#,Sql Server,我想在数据库中插入每个循环的两个变量。怎么做 foreach (string item in split) { val = item; foreach (string uid in useid) { useval = uid; } command = new SqlCommand(); command.CommandText = "Insert_SentSMS"; command.CommandType = CommandType.StoredProcedure; command.Paramet
foreach (string item in split)
{
val = item;
foreach (string uid in useid)
{
useval = uid;
}
command = new SqlCommand();
command.CommandText = "Insert_SentSMS";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@sentTime", sentTime);
command.Parameters.AddWithValue("@mobileNo", val);
command.Parameters.AddWithValue("@userid", useval);
command.Parameters.AddWithValue("@smsType", "Manual");
command.Parameters.AddWithValue("@smsText", smsText);
command.Parameters.AddWithValue("@registrationId", lblCustomSMSRegId.Text);
command.Connection = connection;
command.ExecuteNonQuery();
}
}
我像上面那样尝试过,但“useval”值得到的是相同的值,而不是其他值。如果您可以正确格式化代码,您可以自己找到解决方案 您的代码显然有2个
{
,但有3个}
。这意味着,你要么忘记了你代码中的一个{
,要么你没有提到我们
您正在外部执行命令
foreach(useid中的字符串uid)
语句。这意味着,只有最后一个值将作为useval
执行
将执行代码部分移入到中
foreach (string item in split)
{
val = item;
foreach (string uid in useid)
{
useval = uid;
command = new SqlCommand();
command.CommandText = "Insert_SentSMS";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@sentTime", sentTime);
command.Parameters.AddWithValue("@mobileNo", val);
command.Parameters.AddWithValue("@userid", useval);
command.Parameters.AddWithValue("@smsType", "Manual");
command.Parameters.AddWithValue("@smsText", smsText);
command.Parameters.AddWithValue("@registrationId", lblCustomSMSRegId.Text);
command.Connection = connection;
command.ExecuteNonQuery();
}
}
顺便说一句,不要再使用
AddWithValue
。使用Add
重载来指定您的SqlDbType
和参数大小。还可以使用来处理您的连接和命令。在ever循环中选择或使用useVal
。下面是您可以实现它的方法:
foreach (string item in split)
{
val = item;
foreach (string uid in useid)
{
useval = uid;
command = new SqlCommand();
command.CommandText = "Insert_SentSMS";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@sentTime", sentTime);
command.Parameters.AddWithValue("@mobileNo", val);
command.Parameters.AddWithValue("@userid", useval);
command.Parameters.AddWithValue("@smsType", "Manual");
command.Parameters.AddWithValue("@smsText", smsText);
command.Parameters.AddWithValue("@registrationId", lblCustomSMSRegId.Text);
command.Connection = connection;
command.ExecuteNonQuery();
}
}
但是,我不确定在循环中创建数据库查询是否是一个好主意。您的
foreach
循环是错误的:
foreach (string uid in useid)
{
useval = uid;
}
这没有任何意义(您正在迭代所有项目而不使用它们),您可能打算执行以下操作:
foreach (string item in split)
{
val = item;
foreach (string uid in useid)
{
useval = uid;
command = new SqlCommand();
command.CommandText = "Insert_SentSMS";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@sentTime", sentTime);
command.Parameters.AddWithValue("@mobileNo", val);
command.Parameters.AddWithValue("@userid", useval);
command.Parameters.AddWithValue("@smsType", "Manual");
command.Parameters.AddWithValue("@smsText", smsText);
command.Parameters.AddWithValue("@registrationId", lblCustomSMSRegId.Text);
command.Connection = connection;
command.ExecuteNonQuery();
}
}
问问题时请详细说明。你到底想达到什么目的?正确地放上括号,你就做到了。如果我这样做,那么如何获得不同的“val”值?在val和useval中,我得到了一个值数组,并希望将这些值插入数据库循环wise@dawoodabbas这将在您的
foreach中(string item in split)
语句。这就是为什么您还要迭代item
和uid
的原因。再次检查。那么如何在一个like.foreach(string item in split和string uid in useid)中获得两个变量值呢@dawood:没有人问,你仍然不清楚你的意图是什么。编辑你的问题以给出一个例子。那么:你的意图是什么?如果你实际上在split和useid中有相同数量的项,并且你想保存与索引匹配的对,那么使用for(i=0..sizeof(数组))并通过split[i]和useid[i]访问这两个数组创建查询。