类库文件(DBoper.cs):
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DBManage
{
public class DBoper
{
public static DataSet getCustomers(string ContactTitle, string Conn)
{
string str = "select * from Customers where ContactTitle=@ContactTitle";
SqlConnection sqlConn = new SqlConnection(Conn);
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand(str, sqlConn);
SqlParameter ConTitle = new SqlParameter("@ContactTitle",SqlDbType.NVarChar,20);
ConTitle.Value = ContactTitle;
sqlCmd.Parameters.Add(ConTitle);
SqlDataAdapter sqlAdpor = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
sqlAdpor.Fill(ds,"Customers");
sqlConn.Close();
return ds;
}
}
}
首页文件((Default.aspx):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test.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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
后台文件(Default.aspx,cs):
using System;
using System.Data;
using System.Configuration;
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 DBManage;
using System.Configuration;
namespace test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string contitle;
string Conn = ConfigurationManager.AppSettings["sqlConnectionString"];
contitle = TextBox1.Text;
GridView1.DataSource= DBoper.getCustomers(contitle, Conn);
GridView1.DataBind();
}
}
}
过程:先编写类库,然后将其编译,生成dll文件,并在网页工程中添加引用,最后在工程中调用