我是靠谱客的博主 天真蜜蜂,这篇文章主要介绍建立SQL Server数据库连接实例,现在分享给大家,希望可以做个参考。

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

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

 

 

 

Default.aspx

View Code
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<%@ 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部