简体   繁体   中英

how to save imported excel in datagridview to database C#

how to save imported data from excel in datagridview to database in C# I have saved records and exported to excel sheet, it exported along with data ID, now I have re-imported back to datagridview from excel. now I want to save data to database.

在此处输入图片说明

Important to know:

Database name "Records.sdf" using SQL Compact 3.5 DataGridViewName is RecordsDataGridView.

I'm using following code but it's not working.

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();
    }

ERROR RECEIVING Column Name not Valid, column name = Cash

You should pass varchar field enclosed with single quote.

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

试试这个查询字符串

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

There are a few ways to do this.

Here is one method.

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

You can do this as well.

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();
            }
        }
    }
}

Something like this will work too.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM