如何将更新参数添加到C#中的SQLDataSource [英] How to add update parameter to SQLDataSource in c#
本文介绍了如何将更新参数添加到C#中的SQLDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
参照我的问题,
我试图添加更新参数到一个SQLDataSource使用c#而不是ASP.NET,但我得到错误,
protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
int userID = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Values [userID]。ToString());
string userName =;
string city =;
string updateStatement =更新myTable设置userName = @ userName,city = @ city其中userID = @ userID;
foreach(e.NewValues中的DictionaryEntry条目)
{
if(entry.Key ==userName)
userName = entry.Value.ToString() ;
if(entry.Key ==city)
city = entry.Value.ToString();
using(SqlDataSource ds = new SqlDataSource(ConnectionString(),updateStatement))
{
ds.UpdateParameters.Add(@ userName);
ds.UpdateParameters.Add(@ city);
ds.UpdateParameters.Add(@ UserId);
}
gvDetails.EditIndex = -1;
BindData();
}
如何传递参数值:S
ds.UpdateParameters.Add(@ userName);
ds.UpdateParameters.Add(@ city);
ds.UpdateParameters.Add(@ UserId);
但是我不知道正确的语法,请有人指引我正确的方向
解决方案
我认为您应该使用SqlCommand而不是SqlDataSource:
(新的SqlParameter(@ Name,userName));
cmd.Parameters.Add(new SqlParameter(@ city,city));
cmd.Parameters.Add (new SqlParameter(@ UserId,userID));
cmd.ExecuteNonQuery();
}
In reference to my question,
How to get boundfield value, or am I totally wrong?
I am trying to add update parameter to a SQLDataSource using c# instead of ASP.NET but I am getting error,
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
string userName = "";
string city = "";
string updateStatement = "Update myTable set userName=@userName, city=@city where userID=@userID";
foreach (DictionaryEntry entry in e.NewValues)
{
if (entry.Key == "userName")
userName = entry.Value.ToString();
if (entry.Key == "city")
city = entry.Value.ToString();
}
using (SqlDataSource ds = new SqlDataSource(ConnectionString(), updateStatement))
{
ds.UpdateParameters.Add("@userName");
ds.UpdateParameters.Add("@city");
ds.UpdateParameters.Add("@UserId");
}
gvDetails.EditIndex = -1;
BindData();
}
How can I pass values of parameters :S
ds.UpdateParameters.Add("@userName");
ds.UpdateParameters.Add("@city");
ds.UpdateParameters.Add("@UserId");
But I don't know the right syntax, can someone direct me in right direction please
解决方案
I think you should use an SqlCommand instead of SqlDataSource:
using (SqlConnection connection = new SqlConnection(ConnectionString())
using (SqlCommand cmd = new SqlCommand(updateStatement, connection)) {
connection.Open();
cmd.Parameters.Add(new SqlParameter("@Name", userName));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@UserId", userID));
cmd.ExecuteNonQuery();
}
这篇关于如何将更新参数添加到C#中的SQLDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文