Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新datagridview用作数据库excel文件_C#_Excel_Datagridview_Updates_Oledbconnection - Fatal编程技术网

C# 更新datagridview用作数据库excel文件

C# 更新datagridview用作数据库excel文件,c#,excel,datagridview,updates,oledbconnection,C#,Excel,Datagridview,Updates,Oledbconnection,我已经使用excel将数据添加到datagridview。我不知道如何对datagridview中的所有修改使用更新功能 excel中的选项卡非常简单 ID、材料说明、制造商、制造商零件号供应商、供应商零件号、数量、位置 String name = "Sheet1"; String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "test.xl

我已经使用excel将数据添加到datagridview。我不知道如何对datagridview中的所有修改使用更新功能

excel中的选项卡非常简单 ID、材料说明、制造商、制造商零件号供应商、供应商零件号、数量、位置

        String name = "Sheet1";
        String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        "test.xlsx" +
                        ";Extended Properties='Excel 8.0;HDR=YES;';";

        OleDbConnection con = new OleDbConnection(constr);
        con.Open();
        OleDbCommand oconn = new OleDbCommand("Update [" + name + "$]", con);
        con.Close();
我尝试过这个,没有结果:

            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
            string sql = null;
            MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='test.xlsx';Extended Properties=Excel 8.0;");
            MyConnection.Open();
            myCommand.Connection = MyConnection;
            sql = "Update [Sheet1$] set id = '?' where id=?";
            myCommand.CommandText = sql;
            myCommand.ExecuteNonQuery();
            MyConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
试着这样做:

这是什么语言?C#?请将其添加到您的标签中。
    private void UpadateButton_Click(object sender, EventArgs e)  {
        tran = null;
        excelConnection = new OleDbConnection(connectionString);
        excelConnection.Open(); // This code will open excel file.
        strSQL = "Update [Sheet1$] set Manufacturer= '" + dgvExcelList["Manufacturer",dgvExcelList.CurrentRow.Index].Value.ToString() + "' WHERE ID = '" + dgvExcelList["ID",dgvExcelList.CurrentRow.Index].Value.ToString() + "'";
        try
        {
            tran = excelConnection.BeginTransaction();
            dbCommand = new OleDbCommand(strSQL, excelConnection);
            dbCommand.ExecuteScalar();
            tran.Commit();
        }
        catch(OleDbException ex)
        {
            tran.Rollback();
            MessageBox.Show("error:transaction rolled back," + ex.Message);
        }