VS2010,aspx连接SQLever2008R2,把数据库中的数据展示到网页

7 篇文章 0 订阅

 首先在自己的SQLsever中建立一个叫test1的数据库,

(我的数据库是用的Windows身份验证所以连接字符串后面没有SQLSever的账号和密码)

    protected void Page_Load(object sender, EventArgs e)
    {
        
         SqlConnection conn =new SqlConnection("Integrated Security=SSPI;Data Source=(local);initial catalog=test1");
         Response.Write("状态1:" + conn.State.ToString());
         conn.Open();
         Response.Write("状态2:" + conn.State.ToString());
         conn.Close();
         Response.Write("状态3:" + conn.State.ToString());
    }

运行效果

 10.25更新

改进版,从数据库中读取数据

 1.数据库

 2.demo

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Data Source=(local);initial catalog=test1");
        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from U_table", conn);
        SqlDataReader dataReader = cmd.ExecuteReader();
       
        while (dataReader.Read())
        {
            Response.Write("<center><tr><td></br>"
                + "</td>用户名:<td>" + dataReader["username"].ToString()
                + "</td>密码:<td>" + dataReader["password"].ToString() + "</td></br></tr></center>");
        }
        conn.Close();
    }

 3.显示效果

 

更新10.30

更新版,增加添加数据模块

    protected void btn_insert_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Data Source=(local);initial catalog=test1");
        conn.Open();

        string str_user = txt_user.Text;

        string str_pwd = txt_pwd.Text;

        //插入insert into U_table(username,password) values('2131','3131');
        SqlCommand cmd3 = new SqlCommand("insert into U_table(username,password) values(" + str_user + "," + str_pwd + ")", conn);
        int result = cmd3.ExecuteNonQuery();

     if (result==1)
        {
            tishi.Text = "插入成功";
        }
        else
        {
            tishi.Text = "插入失败";
        }
    }//btn_insert_Click

效果

 

 

要将 Access 数据连接ASPX 页面并显示,你需要使用 ADO.NET 技术。以下是一些步骤: 1. 在 Visual Studio 创建一个 ASP.NET 网站项目。 2. 在项目添加一个数据源。选择 “Microsoft Access 数据库文件” 并指定你的 Access 数据库文件。 3. 在页面上添加一个 GridView 控件,并将数据源与该控件关联。 4. 在代码使用 ADO.NET 连接字符串来连接到 Access 数据库并检索数据。 5. 将数据绑定到 GridView 控件。 以下是一个示例代码: ``` <asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" DataSourceID="accessDataSource"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" /> </Columns> </asp:GridView> <asp:AccessDataSource ID="accessDataSource" runat="server" DataFile="~/App_Data/Northwind.mdb" SelectCommand="SELECT CustomerID, CompanyName, ContactName FROM Customers"></asp:AccessDataSource> ``` 在代码,你可以使用 ADO.NET 连接到 Access 数据库,并检索数据: ``` string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb"; using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand command = new OleDbCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers", connection); connection.Open(); OleDbDataReader reader = command.ExecuteReader(); gridView1.DataSource = reader; gridView1.DataBind(); } ``` 这将从 Access 数据库检索数据,并将其绑定到 GridView 控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值