繁体   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