当前位置:编程学习 > C#/ASP.NET >>

ASP:DataGrid显示数据问题

本人菜鸟 

现在table显示不出来,本地的数据库里有数据,求老师指点

cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;


namespace CH04
{
    public class DataClient
    {

        string connectionString = @"user id=sa;password=Passw02d;initial catalog=pubs;data source=.";

        /// <summary>
        /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SqlDataReader</returns>
        public SqlDataReader ExecuteReader(string strSQL)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(strSQL, connection);
            try
            {
                connection.Open();
                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                throw e;
            }

        }
        /// <summary>
        /// 执行查询语句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public DataTable 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, "Authors");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds.Tables[0];
            }

        }


    }
}


aspx.cs代码

using System;

    using System.Collections;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Web;

    using System.Web.SessionState;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;

    using System.Data.SqlClient;

namespace CH04
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataClient dataClient = new DataClient();
            string strSQL = "";
            System.Data.DataTable dtSoruce = dataClient.Query(strSQL);
            GridView1.DataSource = dtSoruce;
            GridView1.DataBind();



        }
    }
}


aspx代码

<%@ Page language="c#" Codebehind="CH4-1.aspx.cs" AutoEventWireup="false" Inherits="CH04.WebForm1" %>

    <HTML>

    <body MS_POSITIONING="GridLayout">

    <h3><font face="宋体">DataGrid 控件的简单选择</font></h3>

    <ASP:DataGrid id="GridView1" runat="server" EnableViewState="false" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="宋体" CellSpacing="0" CellPadding="3" ShowFooter="false" BorderColor="black" BackColor="#ccccff" Width="592px"></ASP:DataGrid>

    </body>

    </HTML>
--------------------编程问答-------------------- 在线等 --------------------编程问答-------------------- 没人指教么 --------------------编程问答-------------------- 大哥,的查询语句没写啊 
string strSQL = ""; 里面要写你查的内容啊
比如string strSQL = "select * from table" table是你数据库的表名 --------------------编程问答--------------------
引用 3 楼 humiailinxue 的回复:
大哥,的查询语句没写啊 
string strSQL = ""; 里面要写你查的内容啊
比如string strSQL = "select * from table" table是你数据库的表名


加上了,还是无法显示 --------------------编程问答-------------------- 一步一步调试吧,先看看你的数据有没有选出来。 --------------------编程问答-------------------- 求高人指点 --------------------编程问答-------------------- 没有报错,就是数据显示不出 --------------------编程问答-------------------- 无非就是明确下你SQL语句和对应的引用的数据库中是否存在数据 --------------------编程问答-------------------- 调方法的时候 在后面加个[0] 试试。 --------------------编程问答-------------------- http://hi.baidu.com/lnwscn/item/30dbe24e83997aea1e19bcb6
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,