如何将datagridview中的导入excel保存到数据库C#
如何将导入的数据从datagridview中的excel保存到C#中的数据库我已经保存logging并导出到excel表单,它与数据ID一起导出,现在我已经从excel重新导入到datagridview。 现在我想将数据保存到数据库。
重要知道:
数据库名称“Records.sdf”使用SQL Compact 3.5 DataGridViewName是RecordsDataGridView。
我正在使用下面的代码,但它不工作。
public void SaveData() { // Save the data. SqlCeConnection conn = new SqlCeConnection( @"Data Source=|DataDirectory|\Records.sdf;Persist Security Info=False"); SqlCeCommand com; string str; conn.Open(); for (int index = 0; index < RecordsDataGridView.Rows.Count - 1; index++) { str = @"Insert Into OutgoingChequeRecords(ID,BankName,Date,AccountNo, Chequebook, ChequeNo, Payee, Amount, Remarks) Values(" + RecordsDataGridView.Rows[index].Cells[0].Value.ToString() + ", '" + RecordsDataGridView.Rows[index].Cells[1].Value.ToString() + "'," + RecordsDataGridView.Rows[index].Cells[2].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[3].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[4].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[5].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[6].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[7].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[8].Value.ToString() + ")"; com = new SqlCeCommand(str, conn); com.ExecuteNonQuery(); } conn.Close(); }
错误接收 列名无效,列名=现金
试试这个查询string
str = @"Insert Into OutgoingChequeRecords(ID,BankName,Date,AccountNo, Chequebook, ChequeNo, Payee, Amount, Remarks) Values(" + RecordsDataGridView.Rows[index].Cells[0].Value.ToString() + ",'"+ RecordsDataGridView.Rows[index].Cells[1].Value.ToString() + "'," + RecordsDataGridView.Rows[index].Cells[2].Value.ToString() + ",'" + RecordsDataGridView.Rows[index].Cells[3].Value.ToString() + "','" + RecordsDataGridView.Rows[index].Cells[4].Value.ToString() + "','" + RecordsDataGridView.Rows[index].Cells[5].Value.ToString() + "','" + RecordsDataGridView.Rows[index].Cells[6].Value.ToString() + "','" + RecordsDataGridView.Rows[index].Cells[7].Value.ToString() + "','" + RecordsDataGridView.Rows[index].Cells[8].Value.ToString() + "')";
你应该传递用单引号括起来的varchar字段。
var str = @"Insert Into OutgoingChequeRecords(ID,BankName,Date,AccountNo, Chequebook, ChequeNo, Payee, Amount, Remarks) Values(" + RecordsDataGridView.Rows[index].Cells[0].Value.ToString() + ", '" + RecordsDataGridView.Rows[index].Cells[1].Value.ToString() + "'," + RecordsDataGridView.Rows[index].Cells[2].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[3].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[4].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[5].Value.ToString() + "," + "'" + RecordsDataGridView.Rows[index].Cells[6].Value.ToString() + "'" + "," + RecordsDataGridView.Rows[index].Cells[7].Value.ToString() + "," + "'" + dataGridView1.Rows[index].Cells[8].Value.ToString() + "'" + ")";
有几种方法可以做到这一点。
这是一种方法。
private void save_btn_Click(object sender, EventArgs e) { sAdapter.Update(sTable); dataGridView1.ReadOnly = true; save_btn.Enabled = false; new_btn.Enabled = true; delete_btn.Enabled = true; }
http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm
你也可以做到这一点。
string StrQuery; try { using (SqlConnection conn = new SqlConnection(ConnString)) { using (SqlCommand comm = new SqlCommand()) { comm.Connection = conn; conn.Open(); for(int i=0; i< dataGridView1.Rows.Count;i++) { StrQuery= @"INSERT INTO tableName VALUES (" + dataGridView1.Rows[i].Cells["ColumnName"].Text+", " + dataGridView1.Rows[i].Cells["ColumnName"].Text+");"; comm.CommandText = StrQuery; comm.ExecuteNonQuery(); } } } }
这样的事情也会起作用。
private void buttonSave_Click_1(object sender, EventArgs e) // save to invoice { SqlConnection con = new SqlConnection(MyConnectionString); string SqlCmdText = "INSERT INTO invoice (p_code, p_name, p_category, p_price) " + VALUES (@code, @name, @category, @price)"; SqlCommand sc = new SqlCommand(SqlCmdText, con); con.Open(); foreach (DataRow row in MyTable.Rows) { sc.Parameters.Clear(); sc.Parameters.AddWithValue("@code", row["p_code"]); sc.Parameters.AddWithValue("@name", row["p_name"]); sc.Parameters.AddWithValue("@category", row["p_category"]); sc.Parameters.AddWithValue("@price", row["p_price"]); sc.ExecuteNonQuery(); } con.Close(); }
- 如何创buildCSV,以便数字在电子表格中显示为string?
- 如何保存在C#中的Excel文件
- EPPlus:使用外部连接的dynamic数据透视表
- 使用C#读取Excel文件(希望一切都是string)
- 为什么在Excel比较1 <或比较任何数字与任何文字与<结果为真?
- 使用NPOI的.xlsx文件有问题的损坏 – Excel无法打开文件“file.xlsx”,因为文件格式或文件扩展名无效
- 如果我在Visual Studio中开发Excel应用程序,是否会失去macroslogging的好处?
- 将string转换为DateTime时出现奇怪的行为
- closuresxml错误:“删除logging:从/xl/workbook.xml部分命名范围”撇号问题