如何在mvc中编写嵌套的foreach循环 [英] how to write nested foreach loop in mvc

查看:162
本文介绍了如何在mvc中编写嵌套的foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,比如我需要使用两个foreach循环



首先我需要检查记录是否存在于tabel1中,如果现有我需要插入在其他表(table2)中的记录所以这里我使用一个foreach循环,之后我需要检查table2中存在的记录,如果现有的我不会插入记录。

我的代码在下面



I have a requirement like i need to use two foreach loops

first i need to check whether the records are existing in tabel1,if existing i need to insert the record in other table(table2) so here i am using one foreach loop,after that i need to check the records existing in table2 if existing i wont insert the records.
my code is below

cmd1 = new SqlCommand(@"Select id from table3 where id is not null and id in (select id from table1)", conn);
                       dt.Load(cmd1.ExecuteReader());
                       foreach (DataRow rec in dt.Rows)
                       {
                           if (rec["id"].ToString() != null)
                           {

                               cmd1 = new SqlCommand(@"Select id,email from table3 where id is not null and id not in (select id from table2)", conn);

                               dt.Load(cmd1.ExecuteReader());

                               foreach (DataRow row in dt.Rows)
                               {
                                   if (row["id"].ToString() != null)
                                   {
                                      


                                       cmd1 = new SqlCommand("Insert into table2(username,password,email,date_added,forget_password)" +
                                                             " VALUES('" + row["id"].ToString() + "','" + encrypted_pass + "','" +
                                                             row["email"].ToString() + "',getdate(),1)", conn);
                                       cmd1.ExecuteNonQuery();


                                   }

                               }

                           }
                       }

                   }





这里在第一个foreach循环之后它将进入第二个forechaloop,在第一次迭代后它将进入第二个循环而不检查第一个foreach循环(highleted foreach循环)正在迭代),如何做任何人都可以帮助我





提前谢谢

divya



here after first foreach loop it's going second forechaloop, after first iteration it's going to second loop not checking the first foreach loop(highleted foreach loop is iterating), how to do it any one can help me


Thanks in advance
divya

推荐答案

你一直在覆盖cmd1,所以你当然会失去外部循环。只需声明一个新变量并将命令对象分开。
You keep overwriting cmd1 so of course you lose the outer loop. Just declare a new variable and keep your command objects separate.


通常你应该避免重复使用变量,除非你确定它没问题。



As a rule you should avoid re-using variables unless you're sure it's ok.

cmd1 = new SqlCommand(@"Select id from table3 where id is not null and id in (select id from table1)", conn);
                       dt.Load(cmd1.ExecuteReader());
                       foreach (DataRow rec in dt.Rows)
                       {
                           if (rec["id"].ToString() != null)
                           {
                               // use a new SqlCommand here rather that reusing cmd1
                               cmd1 = new SqlCommand(@"Select id,email from table3 where id is not null and id not in (select id from table2)", conn);
                               
                               // create a new DataTable to hold the results rather than reusing dt
                               dt.Load(cmd1.ExecuteReader());
 
                               foreach (DataRow row in dt.Rows)
                               {
                                   if (row["id"].ToString() != null)
                                   {
                                      
 
                                       // use a new SqlCommand here rather that reusing cmd1
                                       cmd1 = new SqlCommand("Insert into table2(username,password,email,date_added,forget_password)" +
                                                             " VALUES('" + row["id"].ToString() + "','" + encrypted_pass + "','" +
                                                             row["email"].ToString() + "',getdate(),1)", conn);
                                       cmd1.ExecuteNonQuery();
 

                                   }
 
                               }
 
                           }
                       }
 
                   }


首先修复 SQL注入 [ ^ ]漏洞。 (如果它在此代码示例中,它可能在任何地方;您需要对代码进行全面审查,以尽快找到并修复漏洞。)



使用块在中包装所有实现 IDisposable 的对象,以确保在所有对象中正确清理其资源案例。



根据您发布的代码,您可以使用单个 INSERT 获得相同的结果查询:

Start by fixing the SQL Injection[^] vulnerability in your code. (If it's in this code sample, it's probably everywhere; you need a full review of your code to find and fix the vulnerability ASAP.)

Wrap all objects that implement IDisposable in a using block, to ensure that their resources are properly cleaned up in all cases.

Based on the code you've posted, you can achieve the same results with a single INSERT query:
const string CommandText = @"INSERT INTO Table2 
(
    username, 
    password, 
    email, 
    date_added, 
    forget_password
)
SELECT
    T.id,
    @password,
    T.email,
    GetDate(),
    1
FROM
    Table3 As T
WHERE
    T.id Is Not Null
And
    Exists
    (
        SELECT 1
        FROM Table1 As I
        WHERE I.id = T.id
    )
And
    Not Exists
    (
        SELECT 1
        FROM Table2 As I
        WHERE I.id = T.id
    )
;";

using (SqlConnection conn = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(CommandText, conn))
{
    cmd.Parameters.AddWithValue("@password", encrypted_pass);
    
    conn.Open();
    cmd.ExecuteNonQuery();
}





现在您需要修改密码存储策略。使用纯文本(或可逆加密)是一种等待发生的灾难。您应该存储密码的盐渍哈希,为每条记录使用唯一的盐,以及多轮强哈希算法。



Now you need to go and fix your password storage strategy. Using plain-text (or reversible encryption) is a disaster waiting to happen. You should be storing a salted hash of the password, using a unique salt for each record, and multiple rounds of a strong hash algorithm.


这篇关于如何在mvc中编写嵌套的foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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