2013-08-19 94 views
0

伙计们我试图将一行DataGridView保存到数据库。保存一行DataGridView到数据库

直到现在我已经得到了一些例子,我们需要指定z INSERT statemnt中的列名和数字。 如下。

string col1 = datagridview1[0, datagridview1s.CurrentCell.RowIndex].Value.ToString(); 
     string col2 = datagridview1[1, datagridview1.CurrentCell.RowIndex].Value.ToString(); 
     string col3 = datagridview1[2, datagridview1.CurrentCell.RowIndex].Value.ToString(); 

     string insert_sql = "INSERT INTO YourTable(col1,col2,col3)VALUES('"+ col1 +"','"+ col2 +"','"+ col3 +"'"); 
     this.getcom(insert_sql); 
     } 
     catch(Exception ex) 
     { Message.Box(ex);} 
    } 

有什么办法可以做INSERT而不必硬编码列的细节? 我想使用datagridview的列标题作为数据库中的列名称将行保存在数据库中。

回答

0

datadapter.update(datatable_name);

+0

请不要简单地发布代码。提供一些关于您的代码的解释或信息或用法。例如,请参阅[此答案](http://stackoverflow.com/a/16893057/756941)。 – NAZIK

相关问题