你好,游客 登录 注册 搜索
背景:
阅读新闻

建立SQL Server数据库连接实例 -

[日期:2013-03-24] 来源:  作者: [字体: ]

ADO.NET对数据库进行访问所用到的主要类:

Connection,DataSet,DataRow,DataColumn,DataRelation,Command,DataAdapter,DataReader,Parameter,Transaction

 

 

 

Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>建立SQL Server数据库连接实例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <div style="text-align: center">
            <table style="width: 778px">
                <tr>
                    <td colspan="4">
                        <asp:Image ID="Image1" runat="server" ImageUrl="~/image/head.gif" /></td>
                </tr>
                <tr>
                    <td colspan="4">
                        <asp:Image ID="Image3" runat="server" ImageUrl="~/image/info.gif" /></td>
                </tr>
                <tr>
                    <td colspan="4" rowspan="1" style="height: 29px">
                        <asp:Button ID="BtnOK" runat="server" OnClick="BtnOK_Click" Text="连接SQLServer数据库显示数据"
                            Width="204px" /></td>
                </tr>
                <tr>
                    <td style="height: 102px;" colspan="4">
                        <asp:GridView ID="GrvInfo" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
                            CellPadding="3" Font-Size="Smaller" Height="151px" OnPageIndexChanging="GrvInfo_PageIndexChanging"
                            PageSize="8" Width="753px">
                            <FooterStyle BackColor="White" ForeColor="#000066" />
                            <Columns>
                                <asp:BoundField DataField="商品名称" HeaderText="信息主题" />
                                <asp:BoundField DataField="门店名称" HeaderText="类型" />
                                <asp:BoundField DataField="销售员" HeaderText="联系人" />
                                <asp:BoundField DataField="销售额" HeaderText="销售额" />
                                <asp:BoundField DataField="利润" HeaderText="利润" />
                            </Columns>
                            <RowStyle ForeColor="#000066" />
                            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px">
                    </td>
                    <td style="width: 100px">
                    </td>
                    <td style="width: 100px">
                    </td>
                    <td style="width: 100px">
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <asp:Image ID="Image2" runat="server" ImageUrl="~/image/foot.gif" /></td>
                </tr>
            </table>
        </div>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void bindGridView()
    {
        string ConStr = System.Configuration.ConfigurationManager.AppSettings["strCon"];
        SqlConnection con = new SqlConnection(ConStr);
        string SqlStr = "select * from tb_sellInfo05";
        SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con);
        con.Open();//打开数据库连接
        DataSet ds = new DataSet();
        ada.Fill(ds);
        GrvInfo.DataSource = ds;
        GrvInfo.DataBind();
        con.Close();//关闭数据库连接
    }
    protected void GrvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GrvInfo.PageIndex = e.NewPageIndex;
        this.bindGridView();

    }
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        try
        {
            this.bindGridView();
            Response.Write("<script language=javascript>alert('恭喜您!SQL Server数据库连接成功!')</script>");
        }
        catch
        {
            Response.Write("<script language=javascript>alert('很遗憾!SQL Server数据库连接失败!')</script>");
        }

    }
}

 






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻