可以用Eval:
<%# DataBinder.Eval(Container.DataItem,"someinteger","{0:c}") %>
7).服务器方数据访问
。连接、命令和数据集(DataSets)
NGWS提供了一系列受管数据访问API,引入名称空间:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
a).生成SQLConnection
b).构造SQLDatasetCommand
c).FillDataSet
如果是插入等操作,则用SQLCommand替换SQLDataSetCommand.
记住:始终在用完连接后关闭连接!否则要等到页面被释放时才会被垃圾回收机制释放资源。
。帮定数据到DataGrid,例:
....
DataSet ds = new DataSet();
myCommand.FillDataSet(ds,"Authors");
MyDataGrid.DataSource = ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
。参数化查询
SQLDataSetCommand支持参数,"@"作为参数前缀,例:
myCommand.SelectCommand.Parameters.Add(new SQLParameter("@state",SQLDataType.VarChar,2));
myCommand.SelectCommand.Parameters["@state"].Value=newValue;
注意,如果DataGrid要在客户与服务器间轮转的话,一定要把MaintainState属性设置为false,
否则,性能受到影响,如:
<asp:DataGrid id="myDataGrid" runat="server"
MaintainState="false"/>
。插入数据
应当保证不为null,可以使用try/catch块。
例:
String insertcmd = "insert into Authors values(@id,@lname)"
SQLCommand myc = new SQLCommand(insertcmd,myConn);
myc.Parameters["@id"].Value=cu_id.Value;
myc.Parameters["@lname"].Value=au_lname.Value;
myc.ActiveConnection.Open();
try{
myc.Execute();
}catch(SQLException e){
if (e.Number==2627)
......//主键重复
}
myc.ActiveConnection.Close();
。更新记录
<%# DataBinder.Eval(Container.DataItem,"someinteger","{0:c}") %>
7).服务器方数据访问
。连接、命令和数据集(DataSets)
NGWS提供了一系列受管数据访问API,引入名称空间:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
a).生成SQLConnection
b).构造SQLDatasetCommand
c).FillDataSet
如果是插入等操作,则用SQLCommand替换SQLDataSetCommand.
记住:始终在用完连接后关闭连接!否则要等到页面被释放时才会被垃圾回收机制释放资源。
。帮定数据到DataGrid,例:
....
DataSet ds = new DataSet();
myCommand.FillDataSet(ds,"Authors");
MyDataGrid.DataSource = ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
。参数化查询
SQLDataSetCommand支持参数,"@"作为参数前缀,例:
myCommand.SelectCommand.Parameters.Add(new SQLParameter("@state",SQLDataType.VarChar,2));
myCommand.SelectCommand.Parameters["@state"].Value=newValue;
注意,如果DataGrid要在客户与服务器间轮转的话,一定要把MaintainState属性设置为false,
否则,性能受到影响,如:
<asp:DataGrid id="myDataGrid" runat="server"
MaintainState="false"/>
。插入数据
应当保证不为null,可以使用try/catch块。
例:
String insertcmd = "insert into Authors values(@id,@lname)"
SQLCommand myc = new SQLCommand(insertcmd,myConn);
myc.Parameters["@id"].Value=cu_id.Value;
myc.Parameters["@lname"].Value=au_lname.Value;
myc.ActiveConnection.Open();
try{
myc.Execute();
}catch(SQLException e){
if (e.Number==2627)
......//主键重复
}
myc.ActiveConnection.Close();
。更新记录
| 对此文章发表了评论 |
