문제

I keep getting an InvalidOperationException ("Update requires a valid UpdateCommand when passed DataRow collection with modified rows"). I just cant work out what's wrong with the update command.

Here's the code I have so far:

    OleDbConnection connection;
    OleDbDataAdapter clientsAdapter new OleDbDataAdapter();
    DataSet myDataSet = new DataSet();

    public void Setup()
    {
        connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Clients.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";";
        connection = new OleDbConnection(connectionString);
        connection.Open();


        // SQL
        clientsAdapter.SelectCommand = new OleDbCommand("SELECT * FROM [Clients$]", connection);
        OleDbCommand updateCmd = new OleDbCommand(
            "UPDATE [Clients$] " + 
            "SET " +
                "[Family Name] = ?, " +
                "[Given Name] = ?, " + 
                "Address = ?, " + 
                "[Home Phone] = ?, " + 
                "[Work Phone] = ?, " + 
                "[Mobile Phone] = ?, " + 
                "Email = ?, " + 
                "Status = ?, " + 
                "Comments = ? " + 
            "WHERE " + 
                "[Last Name] = ? AND " + 
                "[First Name] = ?"

        // SET clause
        updateCmd.Parameters.Add("Family Name", OleDbType.Char, 100, "Family Name");
        updateCmd.Parameters.Add("Given Name", OleDbType.Char, 100, "Given Name");
        updateCmd.Parameters.Add("Address", OleDbType.Char, 100, "Address");
        updateCmd.Parameters.Add("Home Phone", OleDbType.Double, 100, "Home Phone");
        updateCmd.Parameters.Add("Work Phone", OleDbType.Char, 100, "Work Phone");
        updateCmd.Parameters.Add("Mobile Phone", OleDbType.Char, 100, "Mobile Phone");
        updateCmd.Parameters.Add("Email", OleDbType.Char, 100, "Email");
        updateCmd.Parameters.Add("Status", OleDbType.Char, 100, "Status");
        updateCmd.Parameters.Add("Comments", OleDbType.Char, 100, "Comments");

        // WHERE clause
        OleDbParameter fName = updateCmd.Parameters.Add("Old Family Name", OleDbType.Char, 100, "Family Name");
        fName.SourceVersion = DataRowVersion.Original;
        OleDbParameter lName = updateCmd.Parameters.Add("Old Given Name", OleDbType.Char, 100, "Given Name");
        lName.SourceVersion = DataRowVersion.Original;

        clientsAdapter.InsertCommand = updateCmd;

        // create table and fill
        DataTable clients = new DataTable("Clients");
        clientsAdapter.Fill(clients);
        myDataSet.Tables.Add(clients);

        connection.Close();
    }

    public void UpdateDb()
    {
        connection.Open();
        clientsAdapter.Update(myDataSet, "Clients");  // errer occurs here
        connection.Close();
    }

Although there are several simple examples on google, I have not been able to work out a solution.

도움이 되었습니까?

해결책

You haven't set the UpdateCommand property of clientsAdapter.

You've created a OleDbCommand called "updateCmd", but then you've set the InsertCommand property to it:

clientsAdapter.InsertCommand = updateCmd;

I suspect you wanted:

clientsAdapter.UpdateCommand = updateCmd;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top