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

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

问题描述

您好!



所以我正在尝试构建一个允许我将值插入数据库的代码。在执行Insert命令之前,如果存在值,它将检查列值。如果存在该行不会被添加到数据库中,如果它不存在则会插入该行。



我尝试过:



Hello!

So I am trying do build a code which allows me to insert values into my database. Before executing the Insert command it will check on column value if a value exists. If it exists that row won't be added into the database, if it does not exists it will insert the row.

What I have tried:

<pre> for (int row = 0; row< dataGridView1.Rows.Count; row++)
                    {
                        con = new SqlConnection(cs.DBConnPS);
                        con.Open();
                        //I've tried this
                        if (dataGridView1.Rows[linhas].Cells[8].Value.ToString() == "M")
                        {
                            linhas = linhas + 1;
                        }


                        string queryInsert = @"INSERT INTO Table1(Cargs, Refed, Qty, Date, Line, Mark, DescripWeb, CodeProd, TypeEmb, NamePC) VALUES(@Cargs, @Refed, @Qty, @Date, @Line, @Mark, @DescripWeb, @CodeProd, @TypeEmb, @NamePC)";
                        cmd = new SqlCommand(queryInsert);
                        cmd.Connection = con;

                        cmd.Parameters.Add(new SqlParameter("@Cargs", SqlDbType.VarChar, 11, "Cargs"));
                        cmd.Parameters.Add(new SqlParameter("@Refed", SqlDbType.VarChar, 30, "Refed"));
                        cmd.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Decimal, 11, "Qty"));
                        cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.SmallDateTime, 11, "Date"));
                        cmd.Parameters.Add(new SqlParameter("@Line", SqlDbType.SmallInt, 4, "Line"));
                        cmd.Parameters.Add(new SqlParameter("@Mark", SqlDbType.VarChar, 11, "Mark"));
                        cmd.Parameters.Add(new SqlParameter("@DescripWeb", SqlDbType.VarChar, 150, "DescripWeb"));  
                        cmd.Parameters.Add(new SqlParameter("@CodeProd", SqlDbType.VarChar, 20, "CodeProd"));
                        cmd.Parameters.Add(new SqlParameter("@TypeEmb", SqlDbType.VarChar, 8, "TypeEmb"));
                        cmd.Parameters.Add(new SqlParameter("@NamePC", SqlDbType.VarChar, 30, "NamePC"));

                        cmd.Parameters["@Cargs"].Value = dataGridView1.Rows[linhas].Cells[0].Value.ToString();
                        cmd.Parameters["@Refed"].Value = dataGridView1.Rows[linhas].Cells[1].Value.ToString();
                        cmd.Parameters["@Qty"].Value = dataGridView1.Rows[linhas].Cells[2].Value.ToString();
                        cmd.Parameters["@Date"].Value = dataGridView1.Rows[linhas].Cells[3].Value.ToString();
                        cmd.Parameters["@Line"].Value = dataGridView1.Rows[linhas].Cells[4].Value.ToString();
                        cmd.Parameters["@Mark"].Value = dataGridView1.Rows[linhas].Cells[5].Value.ToString();
                        cmd.Parameters["@DescripWeb"].Value = dataGridView1.Rows[linhas].Cells[6].Value.ToString();
                        cmd.Parameters["@CodeProd"].Value = dataGridView1.Rows[linhas].Cells[7].Value.ToString();
                        cmd.Parameters["@TypeEmb"].Value = dataGridView1.Rows[linhas].Cells[8].Value.ToString();
                        cmd.Parameters["@NamePC"].Value = System.Environment.MachineName;

                        cmd.ExecuteNonQuery();
                    }





我该怎么办?



What could I do?

推荐答案

您可以执行类似

You could do something like
string queryInsert = @"IF NOT EXISTS (SELECT Cargs FROM Table1 WHERE Refed=@Refed) BEGIN INSERT INTO Table1(Cargs, Refed, Qty, Date, Line, Mark, DescripWeb, CodeProd, TypeEmb, NamePC) VALUES(@Cargs, @Refed, @Qty, @Date, @Line, @Mark, @DescripWeb, @CodeProd, @TypeEmb, @NamePC) END";



或者首先查询C#代码中的行并使用简单的C#if语句。

示例我假设 Refed 是记录的唯一标识符 - 您可能必须包含其他列以确定确切的行是否已存在


Or just query for the row in your C# code first and use a simple C# if statement.
The example I've given assumes that Refed is the unique identifier for the record - you may have to include other columns to determine if the exact row already exists


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

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