asp.net中怎么使数据库中的表显示在页面上?

asp.net中怎么使数据库中的表显示在页面上?,第1张

using System

using System.Data

using System.Data.SqlClient

using System.Collections

namespace M56kc_Forum

{

/// <summary>

/// DBOperate 的摘要说明。

/// </summary>

public class DBOperate

{

public DBOperate()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

//输入SELECT语句,获得返回数据集

public static DataTable SelectFormForum(string sql,int start,int size,string tablename)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet returns = new DataSet()

sqlAdapter1.Fill(returns,start,size,tablename)

return returns.Tables[0]

}

catch (Exception ex)

{

throw (ex)

}

}

public static DataTable SelectFormForum(string sql)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet returns = new DataSet()

sqlAdapter1.Fill(returns)

return returns.Tables[0]

}

catch (Exception ex)

{

throw (ex)

}

}

//返回一条记录

public static DataRow GetDataRow(string sql,string TableName)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet product = new DataSet()

sqlAdapter1.Fill(product, TableName)

return product.Tables[0].Rows[0]

}

catch (Exception ex)

{

throw (ex)

}

}

//执行SQL语句,如UPDATE,INSERT等

public static void ExcSQL(string s)

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

try

{

SqlCommand sqlCmd =new SqlCommand(s,sqlConnection)

sqlConnection.Open()

sqlCmd.ExecuteNonQuery()

}

catch (Exception ex)

{

throw (ex)

}

finally

{

sqlConnection.Close()

}

}

//获取用户短消息条数

public static int ShortMsgCount(string UserName)

{

try

{

string s="SELECT COUNT(*) AS cun FROM ShortMessage WHERE (systemmsg = 0) AND (Readed = 0) AND (UserName = '"+UserName+"')"

DataRow dr=GetDataRow(s,"ShortMessage")

return System.Convert.ToInt32(dr["cun"].ToString())

}

catch

{

return 0

}

}

//计算总共有多少条问题

public static int CalculateQRecord(int BID,int SID,int solved)

{

try

{

string s="SELECT COUNT(*) AS co FROM Questions WHERE (BigClass_ID = "+BID+") AND (SmallClass_ID = "+SID+") AND (kind >0) AND (kind <5) AND (solved = "+solved+")"

DataRow dr=GetDataRow(s,"Questions")

return System.Convert.ToInt32(dr["co"].ToString())

}

catch

{

return 0

}

}

//计算总共有多少条回复

public static int CalculateARecord(int QID)

{

try

{

string s="SELECT COUNT(*) AS co FROM Answers WHERE (Question_ID = "+QID+")"

DataRow dr=GetDataRow(s,"Answers")

return System.Convert.ToInt32(dr["co"].ToString())

}

catch

{

return 0

}

}

}

}

数据库连接:using System

using System.Collections

using System.Collections.Specialized

using System.Data

using System.Data.SqlClient

using System.Configurationnamespace DAL

{//数据库连接字符串

protected static string connectionString="server=.database=datauid=sapwd=sa"public static string ConnectionString

{

set { connectionString = value}

get { return connectionString}}/// <summary>执行查询语句,返回DataSet

/// </summary>

/// <param name="SQLString">查询语句</param>

/// <returns>DataSet</returns>

public static DataSet Query(string SQLString)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

DataSet ds = new DataSet()

try

{

connection.Open()

SqlDataAdapter command = new SqlDataAdapter(SQLString, connection)

command.Fill(ds, "ds")

}

catch (System.Data.SqlClient.SqlException ex)

{

throw new Exception(ex.Message)

}

return ds

}

} 查询类:using System

using System.Data

using System.Text

using System.Data.SqlClient

using System.Collections

using System.Collections.Generic

using Business.Modelsnamespace Business.SQLServerDAL

{/// <summary>

/// 数据访问类handbook。

/// </summary>

public partial class handbookService

{public handbookService()

{}/// <summary>

/// 获数据列表

/// </summary>

public DataSetGetList()

{

return DbHelperSQL.Query("select * from [2002handbook]")

}}} 页面后台.cshandbookService bll=new handbookService ()GridView1.DataSource=bll.GetList()GridView1.DataBind()

给你个例子吧:

<div>

<ul class="index_news1">

<asp:Repeater ID="rep_CommpanyNewsList" runat="server">

<ItemTemplate>

<li><a href="#" class="index_news_top">

<%# Eval("Title")%></a></li>

</ItemTemplate>

</asp:Repeater>

</ul>

</div>

你看这个,最外层有个div,你可固定这个div的位置,然后里面是ul-li列表,还有个Repeater标签,在后台应该先绑定Repeater,然后<%# Eval("Title")%>这个是需要显示数据源中的某个字段,这里显示的是标题(Title),明白了嘛?你仔细想想,很好明白的!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10056717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存