如何使用OOP在C#中更新我的表?

时间:2014-08-15 02:34:04

标签: c# oop oracle11g crud

我在C#/ .net框架中有一个程序,目前正在实现CRUD(创建,读取,更新,删除)。

以面向对象程序(OOP)格式执行此程序。

到目前为止,我已经实现了Create,Read和Delete功能,并且仍在使用Update。似乎我与我的代码有关,因为我知道我的语法是正确的。

我的桌子无法显示更新的记录。和/或我的表没有更新。

我有一个名为Enrollment和ClassLibrary Module

的项目

注册

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

模块

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

我的其余代码都很好并且编码良好。每次我运行并尝试更新我的记录时,它总是突出显示AccountUpdate函数上的result.ExecuteNonQuery();

enter image description here enter image description here

3 个答案:

答案 0 :(得分:1)

它也缺少where子句。

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

答案 1 :(得分:1)

查看代码,看起来你有一个包装器,变量名result。所以你应该编写类似的代码:

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

答案 2 :(得分:1)

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

删除您的accountID();而是声明参数字符串id并在数据网格中获取id值,这是代码

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

并在datagrid中更新您的数据,请像这样调用ViewList function

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