2016-11-23 96 views
0

如何在excel中將datagridview中的數據保存到數據庫中C# 我保存了記錄並導出到excel表單,它與數據ID一起導出,現在我已經重新導入回到excel的datagridview。現在我想將數據保存到數據庫。如何在datagridview中將導入的excel保存到數據庫C#

enter image description here

重要須知:

數據庫名稱 「Records.sdf」 使用SQL精簡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(); 
    } 

錯誤地收到 列名不合法,列名=現金

+1

缺少某些列中的值!您必須在'收款人(col索引6,也用於其他文本列) – Emanuele

+0

'請幫助日期保存爲0,第2列 – Patrick

+0

每個值必須有格式。使用參數!插入表(列)值(@value)然後設置參數。我無法知道日期的格式。 – Emanuele

回答

1

試試這個查詢字符串

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() + "')"; 
+0

工作但只有1行得到保存。 第3行得到保存,前兩個沒有和日期來到0. – Patrick

+0

修復它保持索引爲0和RecordsDataGridView.Rows.Count -1 RecordsDataGridView.Rows.Count – Patrick

+0

確定日期仍然存在問題。 – Patrick

1

您應該通過隨附單引號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() + "'" + ")"; 
+0

請幫助日期保存爲0,第2列 – Patrick

0

有幾個方法可以做到這一點。

以下是一種方法。

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(); 
} 
相關問題