避免将空行保存到数据库中 [英] avoid saving empty rows into database
本文介绍了避免将空行保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码将数据保存到sql数据库中。每行都有一个唯一的编号(IDENTITY),它可以正常工作,但始终保存gridview上不包含任何数据的最后一行。我如何阻止这个
代码
i use the code below to save data into sql database. each row is given a unique number (IDENTITY) it works alright but always saves the last row on the gridview which does not contain any data. HOW do i prevent this
code
for (int i = 0; i < dgvCategory.Rows.Count; i++)
{
StrQuery = @"INSERT INTO CategorynA VALUES ('" + this.txtCategory.Text + "', '" + dgvCategory.Rows[i].Cells["clmActivity"].Value + "');";
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
推荐答案
for(int i = 0; i< dgvCategory.Rows.Count -1; i ++)< br $>
不要忘记 - 1
for (int i = 0; i < dgvCategory.Rows.Count -1 ; i++)
don't forget the " - 1 "
办理登机手续以确保dgvCategory.Rows [i]。 Cells [clmActivity]。值不为空。
我还建议使用Parameters而不是你正在使用的方法,因为它容易受到SQL注入攻击。
Put a check in to ensure that "dgvCategory.Rows[i].Cells["clmActivity"].Value" is not empty.
I would also suggest using Parameters instead of the approach you are using as it is susceptible to SQL injection attacks.
>
这篇关于避免将空行保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文