简体   繁体   English

如何将datagridview中导入的excel保存到数据库C#

[英]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. 如何将导入数据从datagridview中的excel保存到C#中的数据库中我已保存记录并导出到excel工作表,它与数据ID一起导出,现在我已从excel重新导入回datagridview。 now I want to save data to database. 现在我想将数据保存到数据库。

在此处输入图片说明

Important to know: 重要提示:

Database name "Records.sdf" using SQL Compact 3.5 DataGridViewName is RecordsDataGridView. 使用SQL Compact 3.5 DataGridViewName的数据库名称“ Records.sdf”为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 接收错误 列名称无效,列名称= Cash

You should pass varchar field enclosed with single quote. 您应该传递用单引号引起来的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() + "'" + ")";

试试这个查询字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑后C#保存DataGridView(覆盖导入的Excel文件) - C# save DataGridView after editing (overwrite Excel file that was imported) 如何在C#中将DataGridView多行数据保存到MySQL数据库中 - How to save DataGridView multiple row data in to MySQL database in c# C#如何将多行从Datagridview保存到一个数据库 - C# How to Save multiple rows from Datagridview to One Database 如何在C#中使用datagridview将数据保存到数据库 - How to save data to database using datagridview in C# 如何将数据从DataGridView保存到数据库? C#Winforms - How to save data from a DataGridView into database? c# winforms 如何将datagridview批量数据保存到sql数据库,而datagridview数据与图像绑定到c#中的SQL数据库 - how to save datagridview bulk data to sql database while the datagridview data binded with images to SQL database in c# c#将数据从datagridview保存到数据库 - c# save data from datagridview to database C#如何将文本框中的数据插入到datagridview,然后将datagridview数据保存到数据库中? - C# How to Insert data from text boxes to datagridview and then save the datagridview data into database? c#将datagridview保存到XML以供以后查看Excel - c# save datagridview to XML for later excel viewing 如何在C#中将Excel文件导入DataGridView - How to import an excel file to DataGridView in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM