我的意思是,你是不是把"部门"当成表去检索啦.我给你出你完整的例子,你去改改看, CS: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;namespace promng.page.dicmanage.study { /// <summary> /// study 偺奣梫偺愢柧偱偡丅 /// </summary> public class databind : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid MyDataGrid; private void Page_Load(object sender, System.EventArgs e) { String strConn = "server=zhubin;database=oafinal;uid=sa;pwd="; SqlConnection myConnection = new SqlConnection(strConn); SqlDataAdapter myCommand = new SqlDataAdapter("select * from ques_inf",myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds,"作者"); MyDataGrid.DataSource = ds.Tables["作者"].DefaultView; MyDataGrid.DataBind(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: 偙偺屇傃弌偟偼丄ASP.NET Web 僼僅乕儉 僨僓僀僫偱昁梫偱偡丅 // InitializeComponent(); base.OnInit(e); }
SqlConnection Conn = new SqlConnection(strConn);
Conn.open();
string strSql="select * from 部门";
SqlDataAdapter myCommand = new SqlDataAdapter(strSql, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds,"部门"); DataList1.DataSource=ds.Tables["部门"].DefaultView;
DataList1.DataBind();
Conn.Close();给你写上吧!
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string strSql="select * from 部门";
SqlDataAdapter myCommand = new SqlDataAdapter(strSql, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds,"部门"); DataList1.DataSource=ds.Tables["部门"].DefaultView;
DataList1.DataBind();
Conn.Close();
这样还是空的啊.
{
String strConn = "server=zhubin;database=oafinal;uid=sa;pwd=";
SqlConnection myConnection = new SqlConnection(strConn);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from ques_inf",myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds,"作者"); MyDataGrid.DataSource = ds.Tables["作者"].DefaultView;
MyDataGrid.DataBind();
}这个是我刚试过的,你把数据库的名字和主机名改了,还有相应的字段该啦.
打开连接不用的,我搞错啦!
是什么意思,是说你的数据库里有"部门"这个表,还是你取的代替名,你要是说数据库里有"部门"表,那我就什么都不说啦!
但是datalist显示还是空白.而我"部门"表中明明有十多条数据,好歹也得给我显示两条啊?
这是怎么回事啊?
CS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace promng.page.dicmanage.study
{
/// <summary>
/// study 偺奣梫偺愢柧偱偡丅
/// </summary>
public class databind : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
String strConn = "server=zhubin;database=oafinal;uid=sa;pwd=";
SqlConnection myConnection = new SqlConnection(strConn);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from ques_inf",myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds,"作者"); MyDataGrid.DataSource = ds.Tables["作者"].DefaultView;
MyDataGrid.DataBind();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 偙偺屇傃弌偟偼丄ASP.NET Web 僼僅乕儉 僨僓僀僫偱昁梫偱偡丅
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Designer 僒億乕僩偵昁梫側儊僜僢僪偱偡丅僐乕僪 僄僨傿僞偱
/// 偙偺儊僜僢僪偺僐儞僥儞僣傪曄峏偟側偄偱偔偩偝偄丅
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion }
}HTML<%@ Page language="c#" Codebehind="databind.aspx.cs" AutoEventWireup="false" Inherits="promng.page.dicmanage.study.databind" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>study</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="study" method="post" runat="server">
<asp:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="宋体" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" EnableViewState="false"></asp:DataGrid>
</form>
</body>
</HTML>
<tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr><tr>
<td></td>
</tr>
</table>什么都没有.
还有string strSql="select * from 部门";
这个部门的英文是什么??你写对啦吗?如果都对啦,那我就真的不知道啦!
你的又不是哈希表!
我用了一个datagrid,然后改为
DataGrid1.DataSource=ds.Tables["部门"].DefaultView;
DataGrid1.DataBind ();
这样就可以正常显示,但是datalist就不行.你说设置属性,都改哪些属性啊?我把datalist啥属性也没设置啊,光光的,但是还是空白.这倒底是怎么回事啊?
BorderColor="black"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"> <HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>
<HeaderTemplate>
Items
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</ItemTemplate>
</asp:DataList>
----------------
一个简单示例,将Itemtemplate里面的绑定字段换成你的字段试一下
Droplist,所以很多东西都不懂.
好好学习,天天挣钱!谢谢
请参见:
http://localhost/quickstart/aspplus/doc/webdatabinding.aspx
或者是在帮助中搜索,应当有所收获,下面是aspx代码。
<asp:DataList id="dataList1" runat="server"
RepeatColumns="3"
Width="80%"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0"
>
<ItemTemplate>
Order Date: <%# DataBinder.Eval(Container.DataItem, "DateTimeValue", "{0:d}") %>
<p>
Quantity: <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N2}") %>
<p>
Item: <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
Order Date: <asp:CheckBox id=chk1 Checked='<%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>' runat=server/>
<p>
</ItemTemplate>
</asp:Datalist>
这样做已经很方便了。没有必要*.aspx.cs的代码
请将这句改成:Response.Write (ds.Tables[0].Rows.Count.ToString ()+"============");
看看结果是多少。