我是靠谱客的博主 天真蜜蜂,最近开发中收集的这篇文章主要介绍建立SQL Server数据库连接实例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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>");
        }

    }
}

 


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

最后

以上就是天真蜜蜂为你收集整理的建立SQL Server数据库连接实例的全部内容,希望文章能够帮你解决建立SQL Server数据库连接实例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部