2013-08-19 92 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

相關問題