簡體   English   中英

如何更新數據庫記錄vs12 C#

[英]How to update database record vs12 C#

嗨,我在為網站設置更新按鈕時遇到了麻煩,我嘗試了許多方法,但似乎無法弄清楚。 我希望用戶輸入要更新的客戶端的ID號,然后根據要更改的列填寫文本框。

這是我的代碼:

private static OleDbConnection GetConnection()
{
    String connString;
    connString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\Wisal\Documents\Visual Studio 2012\WebSites\WebSite3\registration-Db.mdb";

    return new OleDbConnection(connString);
}

protected void clientUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection cn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:/Users/Wisal/Documents/Visual Studio 2012/WebSites/WebSite3\registration-Db.mdb");

    cn.Open();
    OleDbCommand cmd = new OleDbCommand("(Update client set id = " + txt_id.Text + " ,[name] ='" + txt_name.Text + " ,[password] ='" + txt_password.Text +" where id= '" + txt_id.Text + "",cn);
    cmd.ExecuteNonQuery();
    cn.Close();
    }      
}

我也嘗試過這個:

private static OleDbConnection GetConnection()
{
    String connString;
    connString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\Wisal\Documents\Visual Studio 2012\WebSites\WebSite3\registration-Db.mdb";

    return new OleDbConnection(connString);

}

protected void clientUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection myConnection = GetConnection();

    myConnection.Open();
    OleDbCommand cmd = new OleDbCommand("(Update client set id = " + txt_id.Text + " ,[name] ='" + txt_name.Text + " ,[password] ='" + txt_password.Text +" where id= '" + txt_id.Text + "",myConnection);
    cmd.ExecuteNonQuery();
    myConnection.Close();
    }
}

有了這個,我得到“聯合查詢中的語法錯誤”。

我也嘗試過這個:

protected void clientUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection myConnection = GetConnection();

    myConnection.Open();
    OleDbCommand cmd = new OleDbCommand("Update client set [name] = ? [password] = ? where id= ?", myConnection);
    cmd.Parameters.AddWithValue("", txt_id.Text);
    cmd.Parameters.AddWithValue("", txt_name.Text);
    cmd.Parameters.AddWithValue("", txt_password.Text);
    cmd.ExecuteNonQuery();
    myConnection.Close();
    }

    }

我已經稍微更新了您的代碼:

private static OleDbConnection GetConnection()
{
    var connString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\Wisal\Documents\Visual Studio 2012\WebSites\WebSite3\registration-Db.mdb";

    return new OleDbConnection(connString);
}

protected void clientUpdate_Click(object sender, EventArgs e)
{
    using(var myConnection = GetConnection())
    {
        myConnection.Open();
        // You should be using a parameterized query here
        using (var cmd = new OleDbCommand("Update client set [name] = ?, [password] = ? where id = ?", myConnection))
        {
            cmd.Parameters.AddWithValue("name", txt_name.Text);
            cmd.Parameters.AddWithValue("password", txt_password.Text);
            cmd.Parameters.AddWithValue("id", txt_id.Text);

            cmd.ExecuteNonQuery();
        }
    }
}
  • 在一次性物品周圍使用磚塊使用
  • 檢查您的SQL,如果這是您的主鍵,則無需更新ID,請注意不要使用單引號。 請改用參數化查詢。

您的單引號不匹配?

您的id字段是int類型還是字符串類型? 我認為它是int類型的。 而且您不需要更新id字段,因為它與where子句中的值相同。

試試這個:

OleDbCommand cmd = new OleDbCommand("Update client set [name] ='" + txt_name.Text + "' ,[password] ='" + txt_password.Text +"' where id= " + txt_id.Text,myConnection);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM