建立SQL Server数据库连接实例
来源:互联网 发布:c语言点滴 百度云 编辑:程序博客网 时间:2024/05/22 15:17
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"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 建立SQL Server数据库连接实例
- Jboss配置 MS SQL SERVER 2008数据库连接池实例
- Jboss配置 MS SQL SERVER 2008数据库连接池实例
- SQL Server 2000数据库连接
- Sql Server 数据库连接问题
- SQL Server数据库连接查询
- SQL Server数据库连接字符串
- SQL server数据库连接
- SQL Server 数据库连接类
- SQL Server数据库连接
- Sql Server 2005数据库连接
- SQL Server数据库连接
- SQL Server关闭数据库连接
- 配置SQL Server数据库连接
- SQL Server 数据库连接池
- PetShop4.0数据库配置 无法连接到 SQL Server 数据库(未能建立数据库连接)的解决方法
- 无法连接到 SQL Server 数据库(未能建立数据库连接)的解决方法
- c# sql server数据库连接类
- CSS:IE,Chrome,Firefox兼容性和CSS Hack
- 负载大逃亡:四十二路怪兽联军及七条逃生法则(很喜欢)
- 关于PHP自定义扩展加载的问题
- Java 程序性能优化
- UVA 574 - Sum It Up
- 建立SQL Server数据库连接实例
- 动态创建的Cstatic Text 响应鼠标点击事件
- 下载安装Cygwin
- c++面试题之内存分配
- Linux编译内核的详细配置
- 《CLR via C#》Part2之Chapter4 类型基础(二)
- uva10905 - Children's Game
- Example 4-6
- Asp.Net请求原理and页面生命周期