SqlDataSource的UpdateCommand参数怎么使用变量?
有一个SqlDataSource
UpdateCommand="update aaabbb set a=@a,b=@b,user1='<%#user1%>',date1='<%#date1%>' where id=@id"
其中:user1 和 date1已经在程序里赋值了。
user1 = Session["username"].ToString() ;
date1 = DateTime.Now.ToString();
可是,写到SQL表里居然是<%#user1%>
意思是ASPX没有编译过来。
请问是怎么回事?我应该怎么解决?谢谢!
------解决方案--------------------UpdateCommand="update aaabbb set a=@a,b=@b,user1= @user1,date1=@date1 where id=@id"
然后在SqlDataSource1_Updating事件中对参数@user1和@date1 进行硬编码赋值
参考:http://eddie005.cnblogs.com/archive/2006/06/27/SetParameters.html
------解决方案--------------------请看以下代码,应该可以实现
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.ConnectionString = @"data source=.;initial catalog=northwind;integrated security=true";
SqlDataSource1.SelectCommand = "select employeeID,FirstName,LastName from employees";
SqlDataSource1.UpdateCommand = "update employees set firstname=@FirstName,lastname=@LastName where employeeid=@EmployeeID";
SqlDataSource1.UpdateParameters.Add("@FirstName", "");
SqlDataSource1.UpdateParameters.Add("@LastName", "");
SqlDataSource1.UpdateParameters.Add("@EmployeeID", "");
if (!IsPostBack)
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.AutoGenerateColumns = false;
GridView1.DataKeyNames = new string[] { "EmployeeID" };
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
GridView1.PageSize = 5;
BoundField bf1 = new BoundField();
BoundField bf2 = new BoundField();
BoundField bf3 = new BoundField();
bf1.HeaderText = "Employee ID";
bf1.DataField = "EmployeeID";
bf1.ReadOnly = true;
bf1.SortExpression = "EmployeeID";
bf2.HeaderText = "First Name";
bf2.DataField = "FirstName";
bf2.SortExpression = "FirstName";
bf3.HeaderText = "Last Name";
bf3.DataField = "LastName";
bf3.SortExpression = "LastName";
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;
GridView1.Columns.Add(bf1);
GridView1.Columns.Add(bf2);
GridView1.Columns.Add(bf3);
GridView1.Columns.Add(cf);
}
}
------解决方案--------------------先问声大家好!
按照cc_qq_yy 的方法 我写了自己的一段代码:
在***.asp.vb中的Page_Load里
Dim st as String =Session("syain_cd").tostring
SqlDataSource1.UpdateCommand = "UPDATE [M_KADAI] SET [kadai_name] = @kadai_name, [update_syain_cd] = " & st.ToString & ", [update_date] = getDate() WHERE [kadai_cd] = @original_kadai_cd"
在***.aspx中
<UpdateParameters>
<asp:Parameter Name="kadai_name" Type="String" />
<asp:Parameter Name="update_syain_cd" Type="String" />
<asp:Parameter Name="update_date" Type="DateTime" />
<asp:Parameter Name="original_kadai_cd" Type="String" />
</UpdateParameters>
数据库更新成功,但st的值的有效位发生了变化,如:
st="0000001" 数据库中的值为"1"
st="0030001" 数据库中的值为"3001"
st="abcde" 数据库更新失败
我的期望st值是 7位字符串型数字 如"0000001"
怎样才能实现我的期望值呢?
如果我的写法有问题,请学长指正,告诉我其他的方法,给UpdateCommand 中的参数赋值。
------解决方案--------------------