C# 如何使用datagridview列检查sql中是否存在行
我试图使用datagridview列检查sql中是否存在一行 我正在使用以下代码行:C# 如何使用datagridview列检查sql中是否存在行,c#,sql,C#,Sql,我试图使用datagridview列检查sql中是否存在一行 我正在使用以下代码行: SqlCommand cmdcontrol= new SqlCommand("checkbarcode", conn); cmdcontrol.CommandType = CommandType.StoredProcedure; cmdcontrol.Parameters.AddWithValue(&qu
SqlCommand cmdcontrol= new SqlCommand("checkbarcode", conn);
cmdcontrol.CommandType = CommandType.StoredProcedure;
cmdcontrol.Parameters.AddWithValue("@barcode", Convert.ToString(tgproduct.Rows[i].Cells[0].Value));
int product = (int)cmdcontrol.ExecuteScalar();
if (product > 0)
{
//update qty of product based on barcode (from column index 0)
}
else
{
//do a insertion process with all details from datagridview
}
请注意,datagridview可能包含未插入sql的产品,以及插入sql且只需更新数量的产品。
我测试了代码以单独执行插入或更新(不是同时执行这两种情况),代码可以工作,但当我混合使用这两种情况时,有一个进程没有完成(更新或插入进程)。
我认为问题出在循环代码中,在循环代码中检查来自datagridview的数据,以查看它们是否存在于sql中(并根据结果完成插入/更新过程)。
有人能帮我解释一下为什么当我把一个产品放在datagridview中,它存在于sql中(并且需要更新数量),而一个procut不存在于sql表中(需要插入产品),这两个过程都不会发生?
感谢大家此代码是如何连接到dgv/其他控制事件的?显示所有相关代码。单击barcoe以检查从datagridview检索到的代码。datagridview中有一个包含产品数据的表。然后使用循环函数,程序必须检查dtg中的数据是否存储在sql中。SqlCommand cmdcontrol=新的SqlCommand(“检查条形码”,conn);//cmdcontrol.CommandType=CommandType.StoredProcess;//cmdcontrol.Parameters.AddWithValue(“@barcode”,Convert.ToString(tgproduct.Rows[i].Cells[0].Value));//int product=(int)cmdcontrol.ExecuteScalar()//