如何执行更新命令 [英] how to Execute update command
本文介绍了如何执行更新命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的问题表中,colums是que_id int,que varchar,ans varchar
i使用下面的代码正在执行,但值没有在表中更新。
//代码是
SqlCommand cmd1 = new SqlCommand();
SqlConnection con = new SqlConnection(@Data Source = .\sqlexpress; Initial Catalog = DB; Integrated Security = True; Pooling = False);
protected void Page_Load(object sender,EventArgs e)
{
cmd1.Connection = con;
con.Open();
string tea_que,tea_ans;
using(cmd1 = new SqlCommand(SELECT que,ans FROM Que1 WHERE que_id = 27,con))
{
using(SqlDataReader reader = cmd1.ExecuteReader())
{
while(reader.Read())
{
tea_que =(string)reader [que]; //用于从表中获取值
tea_ans =(string )读取器[ ANS];
TextBox1.Text = tea_que;
TextBox2.Text = tea_ans;
}
}
//con.Close();
}
}
protected void update_Click(object sender,EventArgs e)
{
try
{
cmd1 = new SqlCommand(UPDATE Que1 SET que ='+ TextBox1.Text +',ans ='+ TextBox2.Text +'WHERE que_id ='+ Label5.Text +',con);
cmd1.Parameters.Add(@ que_id12,SqlDbType.Int).Value = Label5.Text;
cmd1.Parameters.Add(@ que,SqlDbType.VarChar).Value = TextBox1.Text;
cmd1.Parameters.Add(@ que_ans,SqlDbType.VarChar).Value = TextBox2.Text;
// con.Open();
int i = cmd1.ExecuteNonQuery();
con.Close();
Response.Write(已更改);
TextBox1.Text =;
TextBox2.Text =;
}
catch(例外e1)
{
Label3.Text =未成功更新+ e1;
}
}
解决方案
尝试更改此行
cmd1 = new SqlCommand( 更新Que1 SET que =' + TextBox1.Text + ',ans =' + TextBox2.Text + 'WHERE que_id =' + Label5.Text + ',con);
to
cmd1 = new SqlCommand( UPDATE Que1 SET que = @ que,ans = @ que_ans WHERE que_id = @ que_id12,con);
是的,你得到错误的原因是
1.你没有打开连接
2.如果你打开连接那你为什么要在int中存储cmd1.ExecuteNonQuery()?
你在变量i中执行get store并且你没有执行它。
只需删除int我就写了
SqlCommand cmd1 = new SqlCommand();
SqlConnection con = new SqlConnection(@Data Source = .\sqlexpress; Initial Catalog = DB; Integrated Security = True; Pooling = False);
protected void Page_Load(object sender,EventArgs e)
{
cmd1.Connection = con;
con.Open();
string tea_que,tea_ans;
using(cmd1 = new SqlCommand(SELECT que,ans FROM Que1 WHERE que_id = 27 ,con))
{
使用(SqlDataReader reader = cmd1.ExecuteReader())
{
while (读者阅读())
{
tea_que =(string)reader [que]; //用于从表中获取值
tea_ans =(string)reader [ans];
TextBox1.Text = tea_que;
TextBox2.Text = tea_ans;
}
}
//con.Close();
}
}
protected void update_Click(object sender,EventArgs e)
{
尝试
{
cmd1 =新的SqlCommand(UPDATE Que1 SET que =' + TextBox1.Text +',ans ='+ TextBox2.Text +'WHERE que_id ='+ Label5.Text +',con);
cmd1.Parameters.Add( @ que_id12,SqlDbType.Int).Value = Label5.Text;
cmd1.Parameters.Add(@ que,SqlDbType.VarChar).Value = TextBox1.Text;
cmd1.Parameters.Add(@ que_ans,SqlDbType.VarChar).Value = TextBox2.Text;
con.Open();
/ ***** ******如果你不这样做,就不要在int i中存储值。***************
cmd1.ExecuteNonQuery( );
con.Close();
Response.Write(已更改);
TextBox1.Text =;
TextBox2.Text =;
}
catch(例外e1)
{
Label3.Text =未成功更新+ e1;
}
}
cmd1 = new SqlCommand( UPDATE Que1 SET que =' + TextBox1。文本 + ',ans =' + TextBox2 。文本 + 'WHERE que_id = < u>' + Label5。文本 + ',con);
cmd1.Parameters。添加( @ que_id12,SqlDbType。 Int )。Value = Label5。 Text ;
cmd1.Parameters。添加( @ que,SqlDbType。 VarChar )。Value = TextBox1。 Text ;
cmd1.Parameters。添加( @ que_ans,SqlDbType。 VarChar )。Value = TextBox2。 Text ;
带下划线的que-id为int+ label.text +
in my Question table colums are que_id int,que varchar,ans varchar
i am using following code which is executing but values are not updating in table.
//code is
SqlCommand cmd1 = new SqlCommand(); SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DB;Integrated Security=True;Pooling=False"); protected void Page_Load(object sender, EventArgs e) { cmd1.Connection = con; con.Open(); string tea_que, tea_ans; using (cmd1 = new SqlCommand("SELECT que , ans FROM Que1 WHERE que_id =27", con)) { using (SqlDataReader reader = cmd1.ExecuteReader()) { while (reader.Read()) { tea_que = (string)reader["que"];//for fetching values from table tea_ans = (string)reader["ans"]; TextBox1.Text = tea_que; TextBox2.Text = tea_ans; } } //con.Close(); } } protected void update_Click(object sender, EventArgs e) { try { cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con); cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text; cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text; cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text; // con.Open(); int i = cmd1.ExecuteNonQuery(); con.Close(); Response.Write("changed"); TextBox1.Text = ""; TextBox2.Text = ""; } catch (Exception e1) { Label3.Text = "not updated successfully"+e1; } }
解决方案
Try by changing this line
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);
to
cmd1 = new SqlCommand("UPDATE Que1 SET que=@que,ans=@que_ans WHERE que_id=@que_id12", con);
yeah the reason you r getting error is
1. you r not opening the connection
2. if you r opening the connection then Why u r storing cmd1.ExecuteNonQuery() in int i ?
the you r executing get stores in variable i and you are not executing it.
Simply remove int i just write
SqlCommand cmd1 = new SqlCommand();
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DB;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
cmd1.Connection = con;
con.Open();
string tea_que, tea_ans;
using (cmd1 = new SqlCommand("SELECT que , ans FROM Que1 WHERE que_id =27", con))
{
using (SqlDataReader reader = cmd1.ExecuteReader())
{
while (reader.Read())
{
tea_que = (string)reader["que"];//for fetching values from table
tea_ans = (string)reader["ans"];
TextBox1.Text = tea_que;
TextBox2.Text = tea_ans;
}
}
//con.Close();
}
}
protected void update_Click(object sender, EventArgs e)
{
try
{
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);
cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text;
cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text;
cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text;
con.Open();
/*********** Dont store value in int i if you ll do it will not execute your code.***************
cmd1.ExecuteNonQuery();
con.Close();
Response.Write("changed");
TextBox1.Text = "";
TextBox2.Text = "";
}
catch (Exception e1)
{
Label3.Text = "not updated successfully"+e1;
}
}
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con); cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text; cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text; cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text;
underlined que-id is int "+ label.text+"
这篇关于如何执行更新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文