c# access,Dataset及数据绑定

先看下后台代码,数据库用的是access.
designers.aspx.cs:
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Designers : System.Web.UI.Page
{
         protected void Page_Load( object sender, EventArgs e)
        {
                 string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[ "connStr"].ConnectionString;
                OleDbConnection Conn = new OleDbConnection(strConn);
                 string StrComm = string.Empty;
                StrComm = "select    id,Dname,jieshao from [designers] ";
                OleDbDataAdapter Comm = new OleDbDataAdapter(StrComm, Conn);

                DataSet ds = new DataSet();                                                     //DataSet 初试化
                Comm.Fill(ds, "Book");
                designers.DataSource = ds.Tables[ "Book"].DefaultView;
                designers.DataBind();
                     }


         protected string CutLongString( string longString, int charNum)
        {
                 if (longString.Trim().Length > charNum)
                {
                         return longString.Substring(0, charNum);
                }
                 else return longString.Trim();
        }        


}
前台绑定:
<asp:DataList ID= "designers" runat= "server"    RepeatColumns= "2"    
                RepeatDirection= "Horizontal"    
             >
        <ItemTemplate>
        <br />
         <div id= "designtable">
        <div id= "fenleitiaomu"><img src= "designers/<%# Eval("id ")%>.jpg"    alt= "" /> <a href="product.aspx?classid=<%# Eval( "id")%> " target="_blank "><%# Eval("Dname")%></a>
        
        </div>
         <%# CutLongString(DataBinder.Eval(Container.DataItem, "jieshao").ToString(),12)%>        
        <div>    
                 </div>
        </div>

        </ItemTemplate>
        </asp:DataList>
web.config
<connectionStrings>
        <add name= "connStr" connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\data.mdb" providerName= "System.Data.OleDb"/>
    </connectionStrings>

你可能感兴趣的:(C#,职场,休闲)