C# 使用文本框中的值更新数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update database with values from textbox
提问by user175084
i m trying to edit the values in database through textboxes in ASP.
我试图通过 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.
首先,我从数据库中检索值并将这些值设置为表单上文本框的 value 属性,以便用户可以看到旧值。
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();
}
采纳答案by user175084
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();
}
回答by Canavar
You're not providing the @nodeparameter. so you should get an exception. Also change your sql statement like that without parenthesis :
您没有提供@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);
EDIT :As you posted your insert page, your table should have an ID column to identify your record uniquely. As I see in your update SQL youe ID column's name is MachineGroupID. So to update your record, you should provide MachineGroupID as @nodeparameter. try to get this MachineGroupID value in your event and pass it into your Command.
编辑:当您发布插入页面时,您的表应该有一个 ID 列来唯一标识您的记录。正如我在您的更新 SQL 中看到的,您 ID 列的名称是MachineGroupID。因此,要更新您的记录,您应该提供 MachineGroupID 作为@node参数。尝试在您的事件中获取此 MachineGroupID 值并将其传递到您的命令中。
回答by Aryapawan
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);
example :
例子 :
SqlCommand cmdup = new SqlCommand("UPDATE [port1] SET [prt1]=@prt1 WHERE [no]= 1", cn);
cmdup.Parameters.Add("@prt1", TextBox1.Text);
cmdup.ExecuteNonQuery();
I think this may help your case, mention Connection
at the last of your update command
我认为这可能对您的情况有所帮助,请Connection
在更新命令的最后提及