简体   繁体   English

ASP.NET:以编程方式更新sql表行

[英]ASP.NET:Update sql table row programmatically

I have a data conduit class where I want to create an update method that takes list of parameter names, their values and a stored procedure name. 我有一个数据管道类,我想在其中创建一个更新方法,该方法采用参数名称,其值和存储过程名称的列表。 Up on execution, I want to update a particular row in sql db. 在执行时,我想更新sql db中的特定行。

The code so far in data conduit class is: 到目前为止,数据管道类中的代码是:

public class clsDataConduit
{
    SqlConnection Conn= new SqlConnection();
    SqlDataReader rdr= null;
    SqlDataAdapter dataChannel = new SqlDataAdapter();
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder();
    SqlCommand cmd = null;
    List<SqlParameter> SQLParams = new List<SqlParameter>();
    DataTable queryResults = new DataTable();
    DataRow newRecord;


        public void NewRecord(string SProcName)
        {
           //works fine
        }


        public void UpdateRecord(string SProcName)
        {

        Conn= new SqlConnection(connectionString);
        //open the database
        Conn.Open();
        //initialise the command builder for this connection
        SqlCommand dataCommand = new SqlCommand(SProcName, Conn);
        //add the parameters to the command builder
        //loop through each parameter
        for (int Counter = 0; Counter < SQLParams.Count; Counter += 1)
        {
            //add it to the command builder
            dataCommand.Parameters.Add(SQLParams[Counter]);
        }
        dataCommand.CommandType = CommandType.StoredProcedure;
        dataChannel = new SqlDataAdapter(SProcName, Conn);

        dataChannel.UpdateCommand = dataCommand;
        commandBuilder = new SqlCommandBuilder(dataChannel);

        dataChannel.Update(queryResults);

        ///////////////////////////////////////////////////////////
        // Method runs fine till above code, BUT I am not too sure 
        // how to write the rest of the code so that It updates a 
        // a particular row in sql
        ///////////////////////////////////////////////////////////
        //get the structure of a single record
        //newRecord = queryResults.NewRow();   //from previous method - new data


        Conn.Close();
        }
}

I am not too sure how can I continue further from this point. 我不太确定如何从这一点继续。 If someone can help me out please. 如果有人可以帮助我。

Thanks 谢谢

If I understand your code correctly, you have already filled the command with the appropriate parameters (with values already set), so you need only to call 如果我正确理解您的代码,则说明您已经在命令中填充了适当的参数(已设置值),因此您只需调用

dataCommand.ExecuteNonQuery();

This is all you need, (of course the SQLParams list should be already filled with the parameters and their values). 这就是您所需要的(当然,SQLParams列表应该已经填充了参数及其值)。 Also, to be more generic, you could pass to the UpdateRecord, the list of SqlParameter to use for the storedprocedure called 同样,为了更通用,您可以传递给UpdateRecord,这是用于存储过程的SqlParameter列表,称为

public void UpdateRecord(string SProcName, List<SqlParameters> prms)
{
    using(Conn= new SqlConnection(connectionString))
    {
        //open the database
        Conn.Open();
        //initialise the command builder for this connection
        SqlCommand dataCommand = new SqlCommand(SProcName, Conn);
        //add the parameters to the SqlCommand
        dataCommand.Parameters.AddRange(prms.ToArray());
        dataCommand.CommandType = CommandType.StoredProcedure;
        dataCommand.ExecuteNonQuery();
    }
}

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

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