0

我在 MS Access 2007 中创建了一个学生信息小表。表名为“stdinfo”,字段为 rollno、sname、sfather、address。现在我想在 asp.net 应用程序中显示表格字段。

代码背后的代码是

protected void Page_Load(object sender, EventArgs e)
        {
            string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:|Users|mac|Desktop|db.accdb;";
            OleDbConnection cn = new OleDbConnection(ConnectionString);
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select * from stdinfo";
            cmd.Connection = cn;
            cn.Open();
            rolllbl.Text = Convert.ToString(cmd.ExecuteNonQuery());
            cn.Close();
        }

而aspx代码是

<p>
        <asp:Label ID="rolllbl" runat="server" Text="RollNo."></asp:Label>
&nbsp;&nbsp;&nbsp;
    </p>
    <p>
        &nbsp;<asp:Label ID="namelbl" runat="server" Text="Name"></asp:Label>
&nbsp;</p>
    <p>
        <asp:Label ID="fnamelbl" runat="server" Text="Fname"></asp:Label>
&nbsp;&nbsp;&nbsp;
    </p>
    <p>
        <asp:Label ID="addlbl" runat="server" Text="Address"></asp:Label>
    </p>

我不想在 Web 配置文件中写入连接字符串。我正在使用 VS 2010。

我需要在后面的代码中添加什么吗?代码不起作用...

4

2 回答 2

1

你必须改变你的代码。

protected void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:|Users|mac|Desktop|db.accdb;";
        OleDbConnection cn = new OleDbConnection(ConnectionString);
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "Select * from stdinfo";
        cmd.Connection = cn;
        cn.Open();
        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());  // temporary storing data in a data table
        cn.Close();

     // Now assign the values to the label
           rolllbl.Text = dt.Rows[0]["ColumnName"].ToString();
           namelbl.Text = dt.Rows[0]["ColumnName"].ToString();
           fnamelbl.Text = dt.Rows[0]["ColumnName"].ToString();
           addlbl.Text = dt.Rows[0]["ColumnName"].ToString();               

            // OR  If you query returns multiple rows then use gridview 

        gv.DataSource = dt;
        gv.DataBind();  // binding grid   

    }

ExecuteNonQuery方法返回编号。受影响的行数,您将不会通过转换为字符串得到任何东西。你也可以使用gridview控件

<asp:GridView ID="gv" runat="server" >
     <Columns>
         <asp:BoundField DataField="ColumnName" TextField="Roll No" />
         <asp:BoundField DataField="ColumnName" TextField="Name" />
         <asp:BoundField DataField="ColumnName" TextField="First Name" />
         <asp:BoundField DataField="ColumnName" TextField="Address" />
     </Columns>
</asp:GridView>

Output:
Roll NO | Name | First Name | Address
于 2012-08-20T16:37:22.697 回答
0
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>

    <p>
        <asp:GridView ID="GridView1" runat="server">
        <Columns>          
        <asp:BoundField DataField="rollno" HeaderText="Roll" />          
        <asp:BoundField DataField="sname" HeaderText="Student Name" />          
        <asp:BoundField DataField="sfather" HeaderText="Fathers Name" />          
        <asp:BoundField DataField="address" HeaderText="Address" />      
        </Columns>
        </asp:GridView>
    </p>
</asp:Content>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
        {
            string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\mac\\Desktop\\db.accdb;"; 
            OleDbConnection cn = new OleDbConnection(ConnectionString); 
            OleDbCommand cmd = new OleDbCommand(); 
            cmd.CommandText = "Select * from stdinfo";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn;
            cn.Open();
            OleDbDataReader reader ; 
            reader=cmd.ExecuteReader();  // temporary storing data in a data table 

            GridView1.DataSource = reader;
            GridView1.DataBind();
            reader.Close();
            cn.Close();
        }

事情是完美的,但结果显示,即每列显示两次?为什么 ??

于 2012-08-20T20:13:32.777 回答