简体   繁体   English

UpdateCommand不起作用?

[英]UpdateCommand not working?

So I have code that stores an UPDATE query in a string, then I parameter bind the update query and then execute and update it, this is my code: 所以我有将更新查询存储在字符串中的代码,然后我将绑定参数绑定到更新查询,然后执行并更新它,这是我的代码:

string query = "UPDATE Users SET first_name = '@firstname' WHERE ID = @id";
updateUserDS.UpdateParameters.Add("id", HTTPContext.Current.Session["ColumnID"].ToString());
updateUserDS.UpdateParameters.Add("firstname", txt_firstname.Text);
updateUserDS.UpdateCommand = query; 
updateUserDS.Update();

However when I change my string query to: 但是,当我将字符串查询更改为:

string query = "UPDATE Users SET first_name = 'name' WHERE ID = 44";

It works perfectly and updates my table, so I am guessing its something to do with how I have binded the query, does anyone realise anything where I have gone wrong? 它可以完美工作并更新我的表,因此我猜测它与绑定查询的方式有关,是否有人意识到我错了?

BTW: The Session["ColumnID"] is being retrieved perfectly as it states 44 in the stack trace 顺便说一句:Session [“ ColumnID”]被完美检索,因为它在堆栈跟踪中的状态为44

Remove the single quotes from @firstname : 删除@firstname的单引号:

string query = "UPDATE Users SET first_name = @firstname WHERE ID = @id";
updateUserDS.Parameters.AddWithValue("@firstname", first_name);
updateUserDS.Parameters.AddWithValue("@id", HTTPContext.Current.Session["ColumnID"].ToString());

EDIT: 编辑:

Assuming you are using SQL Server as database try like this: 假设您使用SQL Server作为数据库,请尝试如下操作:

SqlCommand sqlComm = new SqlCommand();
sqlComm.CommandText = @"UPDATE Users SET first_name = @firstname WHERE ID = @id";
sqlComm.Parameters.Add("@firstname", SqlDbType.VarChar);
sqlComm.Parameters["@firstname"].Value = txt_firstname.Text;
sqlComm.Parameters.Add("@id", SqlDbType.VarChar);
sqlComm.Parameters["@id"].Value = HTTPContext.Current.Session["ColumnID"].ToString();    

using (SqlConnection sqlConn = new SqlConnection(connection string here);)
{
    sqlComm.Connection = sqlConn;
    sqlConn.Open();
    sqlComm.ExecuteNonQuery();
}

I'd rewrite this as to use OldDbCommand and OleDbConnection instead of SqlDataSource . 我将其重写为使用OldDbCommandOleDbConnection而不是SqlDataSource It doesn't depend on System.Web assembly (smaller deployment size for non-web situations), there's fewer layers in the stack trace (faster), and it's how most people are used to using ADO.NET. 它不依赖于System.Web程序集(对于非Web情况,部署规模较小),堆栈跟踪中的层数更少(更快),这是大多数人习惯使用ADO.NET的方式。

var command = new OleDbCommand("UPDATE Users SET first_name = ? WHERE ID = ?");
command.Parameters.AddWithValue("id", Session["ColumnID"].ToString());
command.Parameters.AddWithValue("firstname", txt_firstname.Text);

using (var connection = new OleDbConnection(connectionString))
{
    command.Connection = connection;
    connection.Open();
    command.ExecuteNonQuery();
}

Another note, you seem to be doing your database access directly in the UI layer! 另外请注意,您似乎正在直接在UI层中进行数据库访问! That's hard to test, and not very flexible. 这很难测试,也不是很灵活。 It'd be better if you moved all data access code into a separate class library, and then communicate back and forth by passing Models , which are code representations of database entities. 最好将所有数据访问代码移至单独的类库中,然后通过传递Models来回通信, Models是数据库实体的代码表示形式。

You're also using Access, which isn't really that great of a database system. 您还使用了Access,它实际上并不是数据库系统的佼佼者。 MS SQL Server is much preferable. MS SQL Server是更可取的。

That might look something like this: 可能看起来像这样:

Code Behind 背后的代码

DataLayer DB {get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    DB = new DataLayer("connectionstring");
}

protected void SubmitBtn_Click(object sender, EventArgs e)
{
    SystemUser user = new SystemUser()
    {
        Id = Session["ColumnID"].ToString(),
        FirstName = txt_firstname.Text
    }
    DB.UpdateUser(user);
}

Data Layer 资料层

public class DataLayer
{
    string ConnectionString {get; set;}

    public DataLayer(string connectionString)
    {
       ConnectionString = connectionString;  
    }

    public void UpdateUser(SystemUser user)
    {
        var command = new SqlCommand("UPDATE Users SET first_name = @firstname WHERE ID = @id");
        command.Parameters.AddWithValue("id", user.Id);
        command.Parameters.AddWithValue("firstname", user.FirstName);

        using (var connection = new SqlConnection(ConnectionString))
        {
            command.Connection = connection;
            connection.Open();
            command.ExecuteNonQuery();
        }
    }

    public void ChangePassword(string UserId, string password)
    {
        //hash and change password here
    }
}

Models 楷模

public class SystemUser
{
    public string Id {get; set;}
    public string FirstName {get; set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM