简体   繁体   中英

How can I update my table in C# with OOP?

I have a program in C#/.net framework and currently implementing CRUD (Create, Read, Update, Delete).

Doing this program in Object Oriented Program (OOP) format.

So far I have implemented the Create, Read and Delete function and still working on Update. It seems that I have something to do with my code as I know my syntax are correct.

My table won't able to show the updated records. And/Or my table is not updating.

I have this project named Enrollment and ClassLibrary Module

Enrollment

AddUser.cs

public void ViewList() {
    DataTable table = new DataTable();
    table = userClass.AccountView();
    dtgRecord.DataSource = table;
}

public void UpdateList() {
    userClass.AccountUpdate(txtUsername.Text, txtPassword.Text, txtPosition.Text);
    MessageBox.Show("Account updated!");
}

private void dtgRecord_CellContentClick(object sender, DataGridViewCellEventArgs e) {
    if (e.RowIndex >= 0) {
        DataGridViewRow row = this.dtgRecord.Rows[e.RowIndex];
        txtUsername.Text = row.Cells["username"].Value.ToString();
        txtPassword.Text = row.Cells["password"].Value.ToString();
        txtPosition.Text = row.Cells["position"].Value.ToString();
    }
}

private void btnUpdate_Click(object sender, EventArgs e) {
    UpdateList();
}

Module

UserClass.cs

public void AccountUpdate(string username, string password, string position) {
    AccountID();
    result.Query = "Update tbl_account set (" + id + ", '" + username + "', '" + password + "', '" + position + "')";
    result.Transaction = true;
    result.ExecuteNonQuery();
    AccountCommit();
    result.Close();
}

public void AccountID() {
    int id = 101;
    result.Query = "Select id from tbl_account order by id asc";
    if (result.Execute()) {
        while (result.Read()) {
            id = result.GetInt("id");
            id++;
        }
    }
}

public void AccountCommit() {
    if (!result.Commit()) {
        result.Rollback();
    }
}

The rest of my codes are good and well coded. Every time I run and try to update my records, It always highlighting the result.ExecuteNonQuery(); on AccountUpdate function.

在此输入图像描述在此输入图像描述

它也缺少where子句。

result.Query="Update tbl_account set username="+username+", password="+password+", position="+position+" where id="+id;

Looking at your code, it looks like you have a wrapper, variable name result . So you should write your code something like:

result.Query="Update tbl_account set username=@0, password=@1, position=@2 where id=@3";
result.Parameters = new Object[] { username, password, position, id };

    public int ExecuteNonQuery() {
        DbProviderFactory dbFact = DbProviderFactories.GetFactory(conn.GetType().Namespace);
        using (DbCommand cmd = dbFact.CreateCommand()) {
            cmd.Connection = conn;

            cmd.CommandText = this.Query;
            for (int i = 0; i < Parameters.Length; i++) {
                var p = dbFact.CreateParameter();
                p.ParameterName = "@" + i;
                p.Value = Parameters[i];
                cmd.Parameters.Add(p);
            }

            return cmd.ExecuteNonQuery();
        }
    }
public void AccountUpdate(string id, string username, string password, string position) {
//remove it AccountID();
    result.Query="Update tbl_account set username='"+username+"', password='"+password+"', position='"+position+"' where id="+id;
//even your id is string if you remove the quote in your where clause, it will still treat as int
    result.Transaction = true;
    result.ExecuteNonQuery();
    AccountCommit();
    result.Close();
}

remove your accountID(); instead declare paramater string id and get the id value in your datagrid here is the code

string id;//declare it as global
private void dtgRecord_CellContentClick(object sender, DataGridViewCellEventArgs e) {
    if (e.RowIndex >= 0) {
        DataGridViewRow row = this.dtgRecord.Rows[e.RowIndex];
        id = row.Cells["id"].Value.ToString();
        txtUsername.Text = row.Cells["username"].Value.ToString();
        txtPassword.Text = row.Cells["password"].Value.ToString();
        txtPosition.Text = row.Cells["position"].Value.ToString();
    }
}

and to update your data in datagrid call your the ViewList function like this

public void UpdateList() {
    userClass.AccountUpdate(id, txtUsername.Text, txtPassword.Text, txtPosition.Text);
    MessageBox.Show("Account updated!");
    ViewList();
}

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