C#For循环使用if语句 [英] C# For loop with if statement

查看:151
本文介绍了C#For循环使用if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  for ( int  i =  0 ;我<  DGStock.Rows.Count; i ++)
{

    conn.Open();
    OleDbCommand cmd2 =  OleDbCommand();
    cmd2 = conn.CreateCommand();
    cmd2.CommandText =  @"  + DGStock.Rows [i] .Cells [" ].Value +  '";
    cmd2.ExecuteNonQuery();
    conn.Close();


    如果(DGStock.Rows [i] .Cells [" ].Value ==   [Location]")
    {
        conn.Open();
        OleDbCommand cmd3 =  OleDbCommand();
        cmd3 = conn.CreateCommand();
        cmd3.CommandText =  @"  + DGStock.Rows [ i] .Cells [" ].Value +  "  + DGStock.Rows [i] .Cells [" span> 条形码"].值+  '";
        cmd3.ExecuteNonQuery();
        conn.Close();
    }
    其他
    {
        conn.Open();
        // 字符串查询= 
        OleDbCommand cmd =  OleDbCommand();
        cmd = conn.CreateCommand();
        cmd.CommandText =  @"  + DGStock .Rows [i] .Cells [" ].Value +  ','" + DGStock.Rows [i] .Cells [ 条形码"].Value +  ')";
        cmd.ExecuteNonQuery();
        conn.Close();
    }

} 


我需要从数据库中选择条形码,然后我需要检查该条形码是否与我从使用的数据网格中向上滚动的条形码具有相同的位置,如果不一致,则需要更新位置,否则只需将位置和条形码添加到数据库中.

解决方案

您好,不确定您遇到的错误是什么,但是我认为您的代码存在一些问题.您似乎在以下代码中匹配了错误的列:

报价:

cmd2.CommandText = @从[条形码]中选择[位置]在[条形码] =''" + DGStock.Rows [i ] .Cells ["Location"].Value +''";



以上不应该是:

报价:

cmd2.CommandText = @从[条形码]中选择[位置] [位置] =''" + DGStock.Rows [i ] .Cells ["Location"].Value +''";



并且您不应该使用ExecuteNonQuery从表中选择数据,它只能由插入,更新和删除以及类似的命令使用:

报价:

cmd2.ExecuteNonQuery();



应该是:

报价:

OledbDataReader rdr = cmd2.ExecuteReader();



然后您应该能够从阅读器的索引访问正确的列.接下来检查以下内容:

报价:

如果(DGStock.Rows [i] .Cells ["Location"].Value =="[Location]")



会变成这样:

报价:

if(DGStock.Rows [i] .Cells ["Location"].Value == rdr ["Location"].ToString( ))



总的来说,我认为代码也可以从重构和整理中受益.但是请先尝试这些更改,然后让我们知道是否可以解决您的问题,如果不能解决,我将删除我的解决方案,我们将重新考虑.


Hi guys i have the following code:

for (int i = 0; i < DGStock.Rows.Count; i++)
{

    conn.Open();
    OleDbCommand cmd2 = new OleDbCommand();
    cmd2 = conn.CreateCommand();
    cmd2.CommandText = @"SELECT [Location] FROM [Barcodes] WHERE [Barcode] = '" + DGStock.Rows[i].Cells["Location"].Value + "'";
    cmd2.ExecuteNonQuery();
    conn.Close();


    if (DGStock.Rows[i].Cells["Location"].Value == "[Location]")
    {
        conn.Open();
        OleDbCommand cmd3 = new OleDbCommand();
        cmd3 = conn.CreateCommand();
        cmd3.CommandText = @"UPDATE [Barcodes] SET [Location] = '" + DGStock.Rows[i].Cells["Location"].Value + "' WHERE [Barcode] = '" + DGStock.Rows[i].Cells["Barcode"].Value + "'";
        cmd3.ExecuteNonQuery();
        conn.Close();                    
    }
    else
    {                   
        conn.Open();
        //string query =
        OleDbCommand cmd = new OleDbCommand();
        cmd = conn.CreateCommand();
        cmd.CommandText = @"INSERT INTO [Barcodes] (Location, Barcode) Values ('" + DGStock.Rows[i].Cells["Location"].Value + "','" + DGStock.Rows[i].Cells["Barcode"].Value + "')";
        cmd.ExecuteNonQuery();
        conn.Close();
    }

}


I need to Select the Barcode from the database, i then need to check if that barcode has the same location as the barcode i''m uploding from the datagrid i''m using, if it is not the same then it needs to update the location, otherwise it just needs to add the location and barcode to the database.

Please if there is any help you can give me?

解决方案

Hi, not sure what error you are getting but I think I see some problems with your code. You seem to be matching the wrong column in the following code:

Quote:

cmd2.CommandText = @"SELECT [Location] FROM [Barcodes] WHERE [Barcode] = ''" + DGStock.Rows[i].Cells["Location"].Value + "''";



shouldn''t the above be:

Quote:

cmd2.CommandText = @"SELECT [Location] FROM [Barcodes] WHERE [Location] = ''" + DGStock.Rows[i].Cells["Location"].Value + "''";



and also you should not be using ExecuteNonQuery to select data from a table, its only used by insert, update and delete and similar commands:

Quote:

cmd2.ExecuteNonQuery();



it should be:

Quote:

OledbDataReader rdr= cmd2.ExecuteReader();



and then you should be able to access the right column from an index from the reader. Following which the following check :

Quote:

if (DGStock.Rows[i].Cells["Location"].Value == "[Location]")



would become this:

Quote:

if (DGStock.Rows[i].Cells["Location"].Value == rdr["Location"].ToString())



Overall i think the code could benefit from a little refactoring and tidying up as well. But do try these changes first and let us know if that solved your problm, if it doesn''t i will delete my solution and we will re-think.


这篇关于C#For循环使用if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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