GridView的绑定数据源(使用三层架构来做)
首先我是定义了一个实体类,代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class Room
{
private int RoomID;
public int RoomID1
{
get { return RoomID; }
set { RoomID = value; }
}
private string Number;
public string Number1
{
get { return Number; }
set { Number = value; }
}
private int TypeID;
public int TypeID1
{
get { return TypeID; }
set { TypeID = value; }
}
private int BedNumber;
public int BedNumber1
{
get { return BedNumber; }
set { BedNumber = value; }
}
private string Description;
public string Description1
{
get { return Description; }
set { Description = value; }
}
private string State;
public string State1
{
get { return State; }
set { State = value; }
}
private int GuestNumber;
public int GuestNumber1
{
get { return GuestNumber; }
set { GuestNumber = value; }
}
}
}
然后我在数据访问层里定义了一个数据访问公共类DBHelper和一个方法
public static class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString;
if (connection == null)//如果连接就状态不存在,就创建一个连接对象并打开连接
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)//如果连接状态是关闭,就打开连接,
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)//如果连接状态中断了,就先关闭连接,再打开连接。
{
connection.Close();
connection.Open();
}
return connection;
}
}
//创建一个SqlDataReader对象
//strSqlCommandSQL语句
//返回SqlDataReader对象
public static SqlDataReader getRead(string strSqlCommand)
{
SqlConnection sqlcon = DBHelper.Connection;//调用了上面一个静态成员的封装属性,只能用类名来调用成员或成员的属性
SqlDataReader sqlread;
//实例化一个SqlCommand对象
SqlCommand sqlcom = new SqlCommand(strSqlCommand, sqlcon);
//ExecuteReader创建SqlDataReader类对象,CommandBehavior .
//CloseConnection说明在关闭SqlDataReader对象的时候,关闭其关联的SqlConnection对象
sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);
return sqlread;
}
因为是我做酒店系统,我想查看房间,所以我在数据访问层里添加了一个房间类,代码:
//功能描述:提供客房信息数据访问
public static class RoomService
{
public static string SelectCommand()
{
string sql="select * from Room";
SqlDataReader red = DBHelper.getRead(sql);
if(red.Read())
{
Room room = new Room();
room.RoomID1 = Convert.ToInt32(red["RoomID"]);
room.TypeID1 = Convert.ToInt32(red["TypeID"]);
room.Number1 = Convert.ToString(red["Number"]);
room.State1 = Convert.ToString(red["State"]);
room.Description1 = Convert.ToString(red["Description"]);
room.BedNumber1 = Convert.ToInt32(red["BedNumber"]);
room.GuestNumber1 = Convert.ToInt32(red["GuestNumber"]);
}
}
我想通过GridView来呈现出查看房间,所以我在GridView的加载事件里打了如下代码
protected void GridView1_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource =RoomService.SelectCommand();
}
}
最后我在GridView1绑定了一个字段,但是显示不出来,求指导啊,我才第一学期学习了,现在要把C#和结合起来,用三层架构做不会啊,求教。
GridView代码如下
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="RoomID" PageSize="17" Width="98%"
Height="243px" onload="GridView1_Load">
<Columns>
<asp:BoundField DataField="Number" HeaderText="房间号" />
<asp:TemplateField HeaderText="客房类型">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="床位数" />
<asp:BoundField HeaderText="客人数" />
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="描述">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/edit.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server"
ImageUrl="~/images/delete.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>