這是一篇關于使用可重用代碼綁定ado數據到控件的文章。
介紹
ado是一種功能非常強大的從數據庫中讀取數據的技術,但是它也使人很容易搞糊涂,連接數據到datagrid或其他控件需要一些技巧和連接方法。我使用的方法是開發標準化的可重用代碼訪問數據庫和顯示數據。我已經寫了很多通過sql語句在datagrid中顯示結果的asp.net頁面。
這篇文章將要描述我是怎樣使用可重用代碼連接ado數據,并在datagrid和其他控件中顯示結果的。我也會講述怎么為類似的任務開發你自己的代碼。
背景
這篇文章假定你已經具有c#,sql,ado和.net控件的知識。
我在演示代碼中使用的是northwind數據庫,但是你可以使用任意的數據庫。
使用代碼web.config
我使用在 web.config 中的 <appsettings> 來保存程序中所要用到的字符串。如果你沒這樣做過,那么你應該試一試。我一般使用 web.config 保存數據庫連接信息,因為這樣可以使它更具有可移植性。
<appsettings>
<add key="dsn_sql"
value="server=localhost;uid=myuser;password=pass;database=northwind;"/>
</appsettings>
datagrid.aspx.cs
下面使 datagrid.aspx 頁面的完整代碼。在這個程序中 bindgrid() 函數的作用使連接到數據庫并在 datagrid 中顯示結果數據。
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.configuration;
namespace easy_ado_binds
{
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
// 從 web.config 獲得連接字符串
public string strconnectsql =
(configurationsettings.appsettings["dsn_sql"]);
private void page_load(object sender, system.eventargs e)
{
// 構造sql字符串
string sqlstring = "select * from employee";
// 調用并構造bindgrid
bindgrid(strconnectsql, sqlstring, datagrid1 );
}
private void bindgrid(string dbconnectstring, string sqlcommand,
system.web.ui.webcontrols.datagrid dgrid)
// 從數據庫中加載初始化頁面
// 綁定到datagrid
{
// 創建數據連接
sqlconnection conn = new sqlconnection(dbconnectstring);
// 調用sql語句
sqlcommand command = new sqlcommand(sqlcommand, conn);
// 創建data adapter
sqldataadapter adapter = new sqldataadapter(command);
// 創建并填充dataset
dataset ds = new dataset();
adapter.fill(ds);
// 填充并綁定到datagrid
dgrid.datasource = ds;
dgrid.databind();
// 關閉連接
conn.close();
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
從 web.config 獲得sql字符串
允許你從 web.config 拿出你所需要的字符串,這是不是很靈活?我用這種方法指定數據庫的連接,報告服務器,主頁默認url字符串以及其他一些全局性的字符串。
using system.configuration;
// 從 web.config 獲得連接字符串
public string strconnectsql = (configurationsettings.appsettings["dsn_sql"]);
private void bindgrid()
這時工程最后做的事情。我把這些代碼放到任意的頁面中,我希望能從自己的數據庫中取到數據并用 datagrid 顯示出來。我不必寫復雜的c#或ado代碼。隨便訪問它,通過數據庫、sql、 datagrid 參數,就為我獲得了數據。
bindgrid() 如何工作
你傳遞給 bindgrid() 一個數據庫連接,一個sql字符串和一個datagrid 標識符,然后它就連接到數據庫,運行sql命令,在datagrid 中顯示數據,最后退出函數。
bindgrid( db, sql, datagrid)
bindgrid( "告訴我是什么數據庫", "告訴我你想運行什么sql語句", "告訴我你想在哪個datagrid中顯示數據")
bindgrid 輸入
private void bindgrid(string dbconnectstring,
string sqlcommand, system.web.ui.webcontrols.datagrid dgrid)
string dbconnectstring: database
string sqlcommand: sql
system.web.ui.webcontrols.datagrid dgrid: datagrid
注意:你在c#中可以為這個函數指定一個web控件作為輸入。所有你必須做的事情是指定哪一個datagrid 是你想要使用這個函數的。
private void bindgrid(string dbconnectstring,
string sqlcommand, system.web.ui.webcontrols.datagrid dgrid)
// 從數據庫中加載初始化頁面
// 綁定到datagrid
{
// 創建數據連接
sqlconnection conn = new sqlconnection(dbconnectstring);
// 調用sql語句
sqlcommand command = new sqlcommand(sqlcommand, conn);
// 創建data adapter
sqldataadapter adapter = new sqldataadapter(command);
// 創建并填充dataset
dataset ds = new dataset();
adapter.fill(ds);
// 填充并綁定到datagrid
dgrid.datasource = ds;
dgrid.databind();
// 關閉連接
conn.close();
}
調用 bindgrid()
函數 bindgrid() 的詳細說明:
數據庫連接字符串:在 web.config 中指定
sql 字符串:任意sql字符串,甚至可以是存儲過程
datagrid: datagrid 的標識符
private void page_load(object sender, system.eventargs e)
{
// 構造sql字符串
string sqlstring = "select * from employee";
// 調用并構造bindgrid
bindgrid(strconnectsql, sqlstring, datagrid1 );
}
使用多個 datagrids
通過不同的sql命令,在頁面上放置三個 datagrid 。如下面所示,只要調用具有不同sql命令的 bindgrid() 三次就可以了。所以現在你可以使用相同的代碼使用多個 datagrid 。
// datagrid 1
string sqlstring1 = "select * from employee";
bindgrid(strconnectsql, sqlstring1, datagrid1 );
// dategrid 2
string sqlstring2 = "select * from customers";
bindgrid(strconnectsql, sqlstring2, datagrid2 );
//datagrid3
string sqlstring3 = "select * from orsders";
bindgrid(strconnectsql, sqlstring3, datagrid3 );
使用 bindlist()
好了。現在我們將從使用 bindgrid() 轉向使用 bindlist() ,它可以使用asp.net中的下拉列表。
代碼稍微有點難理解了,因為 dropdownlist 多了兩個屬性:
datatextfield: 下拉列表中所顯示的,也就是用戶所看到的。
datavaluefield: 測定用戶的選擇的值。
這些值都被添加到 bindlist() 的輸入參數中,所以可以像這樣運行它:
bindlist(db, sql, text, value, dropdownlist);
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.configuration;
namespace bindlist
{
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
// 從 web.config 獲得連接字符串
public string strconnectsql =
(configurationsettings.appsettings["dsn_sql"]);
private void page_load(object sender, system.eventargs e)
{
// 創建sql字符串
string sqlstring = "select employeeid, firstname + ' ' + lastname" +
" as name from employees";
string textfield = "name";
string valuefield = "employeeid";
bindlist(strconnectsql, sqlstring, textfield ,
valuefield, dropdownlist1 );
}
private void bindlist(string strconnectsql, string sqlstring,
string textfield, string valuefield,
system.web.ui.webcontrols.dropdownlist dlist)
{
sqlconnection myconnection = new sqlconnection(strconnectsql);
sqlcommand mycommand = new sqlcommand( sqlstring, myconnection );
myconnection.open();
dlist.datasource = mycommand.executereader();
dlist.datatextfield = textfield;
dlist.datavaluefield = valuefield;
dlist.databind();
myconnection.close();
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
有趣的地方
這樣做的好處之一就是你可以在asp.net中指定 web 控件作為函數的輸入參數。這確實改變了我的編碼習慣,我現在正在開發更多的一般性的可重用代碼。
為什么使用這些代碼
這非常簡單。一旦你要為一個特定的控件編碼,你就不必再重新寫一次了。你可以一次又一次地使用相同的代碼。
歷史
2004年11月 v1.1