使用文本框中的值更新数据库

时间:2009-09-19 23:21:28

标签: c# asp.net

我试图通过ASP中的文本框编辑数据库中的值。

首先我从数据库中重新获取值并将这些值设置为表单上文本框的value属性,以便用户可以看到旧值。

现在,我希望他在相同的文本框中输入新值,当他点击更新时,应该在数据库中更新新值。

任何人都可以告诉我要做什么来获得这些新值???? 何时提交表格????

代码:

protected void Button2_Click(object sender, EventArgs e)
        {
            string MachineGroupName = TextBox2.Text;
            string MachineGroupDesc = TextBox3.Text;
            int TimeAdded = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;

            if (MachineGroupName == "" || MachineGroupDesc == "")
            {
                Label2.Text = ("Please ensure all fields are entered");
                Label2.Visible = true;
            }
            else
            {
                System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
                dataConnection.ConnectionString =
                    @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True";

                System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
                dataCommand.Connection = dataConnection;

                //tell the compiler and database that we're using parameters (thus the @first, @last, @nick)  
                dataCommand.CommandText = ("UPDATE [MachineGroups] SET ([MachineGroupName]=@MachineGroupName,[MachineGroupDesc]=@MachineGroupDesc,[TimeAdded]=@TimeAdded) WHERE ([MachineGroupID]= @node)");

                //add our parameters to our command object  
                dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName);
                dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc);
                dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded);

                dataConnection.Open();
                dataCommand.ExecuteNonQuery();
                dataConnection.Close();

            }

3 个答案:

答案 0 :(得分:1)

您未提供 @node 参数。所以你应该得到一个例外。也可以像没有括号的那样更改你的sql语句:

long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]); 
dataCommand.CommandText = "UPDATE [MachineGroups] SET [MachineGroupName]=@MachineGroupName,[MachineGroupDesc]=@MachineGroupDesc,[TimeAdded]=@TimeAdded WHERE [MachineGroupID]= @MachineGroupID";

//add our parameters to our command object  
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName);
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc);
dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded);
dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID);

编辑:当您发布插入页面时,您的表格应该有一个ID列,以便唯一地标识您的记录。正如我在您的更新中看到的那样,SQL ID ID列的名称是 MachineGroupID 。因此,要更新记录,应将MachineGroupID作为 @node 参数提供。尝试在您的事件中获取此MachineGroupID值并将其传递给您的Command。

答案 1 :(得分:1)

long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]);  
dataCommand.CommandText = "UPDATE [MachineGroups] SET 
[MachineGroupName]=@MachineGroupName,[MachineGroupDesc]=@MachineGroupDesc,    
[TimeAdded]=@TimeAdded WHERE [MachineGroupID]= @MachineGroupID",cn;  //add our parameters to our command object   
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName); 
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc); 
dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded); 
dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID); 

示例:

SqlCommand cmdup = new SqlCommand("UPDATE [port1] SET [prt1]=@prt1 WHERE [no]= 1", cn);             
cmdup.Parameters.Add("@prt1", TextBox1.Text);   
cmdup.ExecuteNonQuery(); 

我认为这可能会对您的情况有所帮助,请在更新命令的最后一次提及Connection

答案 2 :(得分:0)

好的,我的插入页面正常使用此代码.......

    protected void Button2_Click(object sender, EventArgs e)
    {
        string MachineGroupName = TextBox2.Text;
        string MachineGroupDesc = TextBox3.Text;
        int TimeAdded = DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second;

        if (MachineGroupName == "" || MachineGroupDesc == "")
        {
            Label1.Text = ("Please ensure all fields are entered");
            Label1.Visible = true;
        }
        else
        {
            System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString =
                @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True";

            System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = dataConnection;

            //tell the compiler and database that we're using parameters (thus the @first, @last, @nick)  
            dataCommand.CommandText = ("INSERT [MachineGroups] ([MachineGroupName],[MachineGroupDesc],[TimeAdded]) VALUES (@MachineGroupName,@MachineGroupDesc,@TimeAdded)");

            //add our parameters to our command object  
            dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName);
            dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc);
            dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded);

            dataConnection.Open();
            dataCommand.ExecuteNonQuery();
            dataConnection.Close();

        }