链接数据库
SqlAccessConnection.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlAccessConnection.aspx.cs" Inherits="SqlAccessConnection" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
1、<asp:Button ID="Button1" runat="server" Text="连接" OnClick="Button1_Click"/><br /><br />
2、<asp:Button ID="Button2" runat="server" Text="连接,绑定" OnClick="Button2_Click"/>
<asp:GridView ID="gvTest" runat="server"></asp:GridView>
3、<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="100px">
<Columns>
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConnectionStrings %>"
SelectCommand="SELECT top 3 * FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlAccessConnection.aspx.cs" Inherits="SqlAccessConnection" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
1、<asp:Button ID="Button1" runat="server" Text="连接" OnClick="Button1_Click"/><br /><br />
2、<asp:Button ID="Button2" runat="server" Text="连接,绑定" OnClick="Button2_Click"/>
<asp:GridView ID="gvTest" runat="server"></asp:GridView>
3、<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="100px">
<Columns>
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConnectionStrings %>"
SelectCommand="SELECT top 3 * FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
SqlAccessConnection.aspx.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SqlAccessConnection : System.Web.UI.Page
{
protected string CONN_STRING_NON_DTC = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"];
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
//连接字符串1
string str1="server=(local);Database=test;Integrated Security=true;";
//连接字符串2
string str2="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa";
//使用Web.Config文件中的
string str3 = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"];
SqlConnection conn = new SqlConnection(str1);//str2,str3,都可以
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select top 3 * from students";
cmd.Connection = conn;
//SqlCommand cmd = new SqlCommand("Select * from students", conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.Write(reader["name"] + " ");
}
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=(local);Database=test;Integrated Security=true;";//
SqlCommand cmd = new SqlCommand("Select top 3 * from students", conn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
conn.Open();
DataTable table = new DataTable();
DataSet dsDataSet = new DataSet();
adapter.Fill(dsDataSet, "table");
conn.Close();
table = dsDataSet.Tables["table"];
this.gvTest.DataSource = table.DefaultView;
this.gvTest.DataBind();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SqlAccessConnection : System.Web.UI.Page
{
protected string CONN_STRING_NON_DTC = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"];
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
//连接字符串1
string str1="server=(local);Database=test;Integrated Security=true;";
//连接字符串2
string str2="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa";
//使用Web.Config文件中的
string str3 = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"];
SqlConnection conn = new SqlConnection(str1);//str2,str3,都可以
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select top 3 * from students";
cmd.Connection = conn;
//SqlCommand cmd = new SqlCommand("Select * from students", conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.Write(reader["name"] + " ");
}
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=(local);Database=test;Integrated Security=true;";//
SqlCommand cmd = new SqlCommand("Select top 3 * from students", conn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
conn.Open();
DataTable table = new DataTable();
DataSet dsDataSet = new DataSet();
adapter.Fill(dsDataSet, "table");
conn.Close();
table = dsDataSet.Tables["table"];
this.gvTest.DataSource = table.DefaultView;
this.gvTest.DataBind();
}
}
Web.Config
Code
<connectionStrings>
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="SqlAppSettings" value="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa"/>
</appSettings>
<connectionStrings>
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="SqlAppSettings" value="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa"/>
</appSettings>