注册 登录
编程论坛 ASP.NET技术论坛

GridView无法显示

yxhrgyj 发布于 2012-03-23 16:52, 793 次点击
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlDataReader MyReader;
            SqlConnection MyConnection = new SqlConnection();
            MyConnection.ConnectionString = (@"Data Source=RGYJ-AVTYN682XD;Initial Catalog=库;User ID=sa;Password=sa123456789");
            SqlCommand MyCommand = new SqlCommand();
             = "SELECT * FROM 销售";
             = CommandType.Text;
            MyCommand.Connection = MyConnection;
            MyCommand.Connection.Open();
            //Response.Write("连接成功");
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = MyReader;
            GridView1.DataBind();
        }
    }


运行以后为什么 GridView控件部分显示是空白 求解释
4 回复
#2
guming2012-03-26 05:23
把代码改成以下试试:
 ...
 MyReader = MyCommand.ExecuteReader();
 GridView1.DataSource = MyReader;
 GridView1.DataBind();

 MyReader.Close();
 MyConnection.Close()
#3
yhmm2012-03-27 13:10
过滤转化了

#4
hhy4204596742012-03-27 16:28
我用你的代码测试过,如果你的连接是成功了的,并且数据库中有数据,那么是可以显示出结果的。
测试连接是否成功的代码:
if (!Page.IsPostBack)
        {
            SqlDataReader MyReader;
            SqlConnection MyConnection = new SqlConnection();
            MyConnection.ConnectionString = (@"Data Source=RGYJ-AVTYN682XD;Initial Catalog=库;User ID=sa;Password=sa123456789");
            SqlCommand MyCommand = new SqlCommand();
             = "SELECT * FROM 销售";
             = CommandType.Text;
            MyCommand.Connection = MyConnection;
            MyCommand.Connection.Open();
            if (MyConnection.State == ConnectionState.Open)
                Response.Write("连接成功");
            else
               Response.Write("连接失败");            
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = MyReader;
            GridView1.DataBind();
            MyReader.Close();
            MyReader.Dispose();
        }


看看链接数据库是不是成功的
#5
tongxin7202012-03-27 20:14
呵呵
1