Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中以循环方式获取两个变量值?_C#_Sql Server - Fatal编程技术网

如何在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]访问这两个数组创建查询。