1

我有一个数据网格(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

}

我无法进行循环。

请帮忙。

4

1 回答 1

1

我假设您已row_no根据 grid_detail 行数设置,然后进行以下更改并测试

for (int r=0; r< grid_detail.RowCount; r++)
{
    // your code...
    //int r = 0; comment this line 
    // your code...
    //.............
            for (i = 0; i < col_no; i++) 
            {
                //fill in the Array with values from cells
                col_value[i] = Convert.ToString(grid_detail.Rows[r].Cells[i].Value); // use row r 
            }
    //.............
    // your code...

}

但我会将您的代码重新考虑到下面

foreach (GridViewRow row in GridView1.Rows)
{
    string sql = GenerateSql(row);
    if (!string.IsNullOrEmpty(sql))
        ExecuteNonQuery(sql);
}
private void ExecuteNonQuery(string query)
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand cmd_server = new SqlCommand(query, conn))
    {
        conn.Open();
        cmd_server.ExecuteNonQuery();
    }
}

private string GenerateSql(GridViewRow row)
{
    var values = row.Cells.Cast<TableCell>().Select(x => x.Text).ToArray();
    if (values.Any(string.IsNullOrEmpty))
    {
        return string.Empty;
    }
    var sql = string.Format("INSERT INTO  [Ref].[{0}] VALUES ({1})", _lstview_item, string.Join(",", values));

    return sql;    

}
于 2013-08-22T05:34:07.870 回答