更新数据库从文本框的值 [英] Update database with values from textbox
问题描述
读音字试图通过文本框的ASP编辑在数据库中的值。
首先我retrived从数据库中的值,并设置表格上的数值文本框的值属性,以便用户可以看到旧值。
现在,我希望他在同一个文本框中输入新值,当他在更新单击新值应在数据库中更新。
任何一个可以告诉我有什么做的就是这些新值????
当提交表单????
在code:
保护无效Button2_Click(对象发件人,EventArgs的发送)
{
字符串MachineGroupName = TextBox2.Text;
字符串MachineGroupDesc = TextBox3.Text;
INT TimeAdded = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second; 如果(MachineGroupName ==|| MachineGroupDesc ==)
{
Label2.Text =(请确保输入的所有字段);
Label2.Visible = TRUE;
}
其他
{
System.Data.SqlClient.SqlConnection dataConnection =新的SqlConnection();
dataConnection.ConnectionString =
@数据源= JAGMIT-PC \\ SQLEX $ P $干燥综合征;初始目录= SumooHAgentDB;集成安全性=真; System.Data.SqlClient.SqlCommand dataCommand =新的SqlCommand();
dataCommand.Connection = dataConnection; //告诉编译器和我们使用的参数数据库(因此@First,@Last,@nick)
dataCommand.CommandText =(UPDATE [MachineGroups] SET([MachineGroupName] = @ MachineGroupName,[MachineGroupDesc] = @ MachineGroupDesc,[TimeAdded] = @ TimeAdded)WHERE([MachineGroupID] = @node)); //我们的参数添加到我们的命令对象
dataCommand.Parameters.AddWithValue(@ MachineGroupName,MachineGroupName);
dataCommand.Parameters.AddWithValue(@ MachineGroupDesc,MachineGroupDesc);
dataCommand.Parameters.AddWithValue(@ TimeAdded,TimeAdded); dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close(); }
确定我有一个是工作的罚款与此code插入页面.......
保护无效Button2_Click(对象发件人,EventArgs的发送)
{
字符串MachineGroupName = TextBox2.Text;
字符串MachineGroupDesc = TextBox3.Text;
INT TimeAdded = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second; 如果(MachineGroupName ==|| MachineGroupDesc ==)
{
Label1.Text =(请确保输入的所有字段);
Label1.Visible = TRUE;
}
其他
{
System.Data.SqlClient.SqlConnection dataConnection =新的SqlConnection();
dataConnection.ConnectionString =
@数据源= JAGMIT-PC \\ SQLEX $ P $干燥综合征;初始目录= SumooHAgentDB;集成安全性=真; System.Data.SqlClient.SqlCommand dataCommand =新的SqlCommand();
dataCommand.Connection = dataConnection; //告诉编译器和我们使用的参数数据库(因此@First,@Last,@nick)
dataCommand.CommandText =(插入[MachineGroups]([MachineGroupName],[MachineGroupDesc],[TimeAdded])值(@ MachineGroupName,@ MachineGroupDesc,@ TimeAdded)); //我们的参数添加到我们的命令对象
dataCommand.Parameters.AddWithValue(@ MachineGroupName,MachineGroupName);
dataCommand.Parameters.AddWithValue(@ MachineGroupDesc,MachineGroupDesc);
dataCommand.Parameters.AddWithValue(@ TimeAdded,TimeAdded); dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close(); }
i m trying to edit the values in database through textboxes in ASP.
first i retrived the values from database and set those values to the value property of textboxes on the form so that user can see the old values.
now, i want him to enter new values in the same textboxes and when he click on update the new values should be updated in the database.
can any one tell what i have to do to get those new values???? when to submit the form????
the code:
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();
}
ok i have the insert page which is working fine with this code.......
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();
}
这篇关于更新数据库从文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!