我有一个数据网格(grid_detail),因为现在我只读取它的一行并使用下面的代码保存到数据库
//number of columns actually in second dgv
int col_no = grid_detail.ColumnCount;
int row_no = grid_detail.RowCount - 1;
int r = 0;
//array to contain the values of the cells
string[] col_value = new string[100];
for (i = 0; i < col_no; i++)
{
//fill in the Array with values from cells
col_value[i] = Convert.ToString(grid_detail.Rows[0].Cells[i].Value);
}
//Build insert command
StringBuilder Q = new StringBuilder();
Q.Append("INSERT INTO ");
Q.Append("[Ref].[");
Q.Append(_lstview_item);
Q.Append("]");
Q.Append(" VALUES");
Q.Append("(");
for (i = 0; i < col_no; i++)
{
Q.Append("'");
if (string.IsNullOrEmpty(col_value[i]))
{
x = false;
break;
}
else
{
Q.Append(col_value[i]);
Q.Append("'");
Q.Append(",");
x = true;
}
}
//removing last COMMA from loop
if (col_no > 0)
{
Q.Length--;
}
Q.Append(")");
query = Q.ToString();
if (x == true)
{
//open connection to db and send query
SqlConnection conn = new SqlConnection(cc.connectionString(cmb_dblist.Text));
SqlCommand cmd_server = new SqlCommand(query);
cmd_server.CommandType = CommandType.Text;
cmd_server.Connection = conn;
conn.Open();
cmd_server.ExecuteNonQuery();
conn.Close();
但是,我的要求是我现在读取不止一行 z 网格并保存到数据库。我尝试使用 for 循环来获取行数,例如
for (r=0;r<row_no;r++)
{
/////CODES ABOVE PASTED HERE
}
我无法进行循环。
请帮忙。