在VS 2010中将asp.net与MS access 2007连接起来

时间:2012-08-20 16:09:55

标签: asp.net database ms-access-2007

我在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。

我需要在后面的代码中添加任何内容吗?代码不起作用......

2 个答案:

答案 0 :(得分: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方法返回no。受影响的行,您将无法通过转换为字符串获得任何内容。 您还可以使用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

答案 1 :(得分: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();
        }

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