0

我正在尝试用来自数据库视图的数据填充 Gridview。我不能使用 linq,因为视图有 200+k 行我必须显示。这是我的代码:

     public partial class _Default : System.Web.UI.Page
{
    private string mobileGateway = "MobileGateway";
    private List<string> addressReport = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        GetReport(addressReport);
    }

    public void GetReport(List<string> adr)
    {
        string connecntionString = ConfigurationManager.ConnectionStrings[mobileGateway].ConnectionString;

        using (SqlConnection connection = new SqlConnection(connecntionString))
        {
            try
            {
                connection.Open();
                string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
                using (SqlCommand command = new SqlCommand(sqlCmd, connection))
                {
                    command.CommandType = System.Data.CommandType.Text;
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            adr.Add("" + reader[0]);
                            adr.Add("" + reader[1]);
                            adr.Add("" + reader[2]);
                            adr.Add("" + reader[3]);
                            adr.Add("" + reader[4]);
                            adr.Add("" + reader[5]);
                            adr.Add("" + reader[6]);
                            adr.Add("" + reader[7]);
                            adr.Add("" + reader[8]);
                            adr.Add("" + reader[9]);
                            adr.Add("" + reader[10]);
                        }

                        Grid.DataSource = adr;
                        Grid.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR!!!!: " + ex.Message);
            }
        }
    }
}

}

asp代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AddressReporting._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="Grid" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False">
</asp:GridView>
</asp:Content>

我得到一个完全没有gridview 的空白页。我究竟做错了什么 ?

4

4 回答 4

2
enter code here

command.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
Grid.DataSource = reader;
Grid.DataBind();

试试这个,它会将所有细节输入阅读器并将其直接绑定到网格。除非您想在那里做其他事情,否则无需遍历所有行。

于 2012-10-30T08:30:49.937 回答
1

尝试这样的事情:

string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
using (SqlCommand command = new SqlCommand(sqlCmd, connection))
{
     DataTable dataTable = new DataTable();
     command.CommandType = System.Data.CommandType.Text;
     connection.Open();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

     dataAdapter.Fill(dataTable);

     Grid.DataSource = dataTable;
     Grid.DataBind();
}
于 2012-11-01T00:14:45.637 回答
0

尝试声明 ref

public void GetReport(ref List<string> adr)

参考(C# 参考)

于 2012-10-30T14:20:23.847 回答
0

检查您的查询是否正在获取数据,因为您看不到空的 GridView

于 2012-10-30T14:11:11.190 回答