根据C#中列的值转到下一行 [英] Go to the next row depending on a value from a column in C#

查看:75
本文介绍了根据C#中列的值转到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试构建一段代码,我可以根据列中的值将该行插入或不插入数据库。基本上它会检查我试图插入的所有行,看看是否有任何一个带有值列的行,在这种情况下,带有M。



我尝试过:



So, I am trying to build a piece of code where I am able to depending on a value from a column it will insert that row or not into the database. Basically it will check all the rows that I am trying to insert and see if that is any one with a value column, in this case, with "M".

What I have tried:

<pre>for (int linhas = 0; linhas < dataGridView1.Rows.Count; linhas++)
                    {
                        if (dataGridView1.Rows[linhas].Cells[8].Value.ToString() != "M")
                        {

                            string queryInsert = @"INSERT INTO CIM_Etiquetas(Carga, Referencia, Quantidade, DataEmissao, Linha, Marca, DescricaoWeb, CodProd, TipoEmb, NomePC) VALUES (@Carga, @Referencia, @Quantidade, @DataEmissao, @Linha, @Marca, @DescricaoWeb, @CodProd, @TipoEmb, @NomePC)";
                            cmd = new SqlCommand(queryInsert);
                            cmd.Connection = con;

                            cmd.Parameters.Add(new SqlParameter("@Carga", SqlDbType.VarChar, 11, "Carga"));
                            cmd.Parameters.Add(new SqlParameter("@Referencia", SqlDbType.VarChar, 30, "Referencia"));
                            cmd.Parameters.Add(new SqlParameter("@Quantidade", SqlDbType.Decimal, 11, "Quantidade"));
                            cmd.Parameters.Add(new SqlParameter("@DataEmissao", SqlDbType.SmallDateTime, 11, "DataEmissao"));
                            cmd.Parameters.Add(new SqlParameter("@Linha", SqlDbType.SmallInt, 4, "Linha"));
                            cmd.Parameters.Add(new SqlParameter("@Marca", SqlDbType.VarChar, 11, "Marca"));
                            cmd.Parameters.Add(new SqlParameter("@DescricaoWeb", SqlDbType.VarChar, 150, "DescricaoWeb"));
                            cmd.Parameters.Add(new SqlParameter("@CodProd", SqlDbType.VarChar, 20, "CodProd"));
                            cmd.Parameters.Add(new SqlParameter("@TipoEmb", SqlDbType.VarChar, 8, "TipoEmb"));
                            cmd.Parameters.Add(new SqlParameter("@NomePC", SqlDbType.VarChar, 30, "NomePC"));

                            cmd.Parameters["@Carga"].Value = dataGridView1.Rows[linhas].Cells[0].Value.ToString();
                            cmd.Parameters["@Referencia"].Value = dataGridView1.Rows[linhas].Cells[1].Value.ToString();
                            cmd.Parameters["@Quantidade"].Value = dataGridView1.Rows[linhas].Cells[2].Value.ToString();
                            cmd.Parameters["@DataEmissao"].Value = dataGridView1.Rows[linhas].Cells[3].Value.ToString();
                            cmd.Parameters["@Linha"].Value = dataGridView1.Rows[linhas].Cells[4].Value.ToString();
                            cmd.Parameters["@Marca"].Value = dataGridView1.Rows[linhas].Cells[5].Value.ToString();
                            cmd.Parameters["@DescricaoWeb"].Value = dataGridView1.Rows[linhas].Cells[6].Value.ToString();
                            cmd.Parameters["@CodProd"].Value = dataGridView1.Rows[linhas].Cells[7].Value.ToString();
                            cmd.Parameters["@TipoEmb"].Value = dataGridView1.Rows[linhas].Cells[8].Value.ToString();
                            cmd.Parameters["@NomePC"].Value = System.Environment.MachineName;

                            cmd.ExecuteNonQuery();
                        }
                    }





如果该行作为具有该值的列,它将跳转到下一行,直到都结束了。你有任何想法我该如何解决?



If that row as a column with that value it will jump to the next row until it's finished. Do you have any ideas how can I solve it?

推荐答案

如果我理解你的问题,你想跳过某一列中有M的行。



更改你的if语句:



If I understand your question, you want to skip rows that have an "M" in a certain column.

Change your if statement from this:

if ((Myrow.Cells[8].Value.ToString()) == "M")
{
    //What should I do here?
}
else
{





...对此:





...to this:

if (Myrow.Cells[8].Value.ToString()) != "M")
{



只会在第8列中插入不具有M值的行,并且不会插入如果他们这样做的话,请等待他们。



如果那不是你的意思,请改进你的问题。


This will only insert rows that do not have a value of M in the 8th column, and will not insert them if they do.

If that's NOT what you meant, improve your question.


这篇关于根据C#中列的值转到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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