国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

如何使用C#創(chuàng)建一個三層的數(shù)據(jù)庫應用程序

2024-07-21 02:19:56
字體:
來源:轉載
供稿:網(wǎng)友

商業(yè)源碼熱門下載www.html.org.cn

如何使用c#創(chuàng)建一個三層的數(shù)據(jù)庫應用程序
1.分析
在我們這個程序中采用如下的層次:web層,業(yè)務實體層,數(shù)據(jù)層。
其中:
業(yè)務實體層負責web層與數(shù)據(jù)層之間的數(shù)據(jù)交換。
數(shù)據(jù)層僅僅代表數(shù)據(jù)庫。
web層通過業(yè)務實體層來訪問數(shù)據(jù)庫。
我們的中間的業(yè)務實體層采用webservice.
2.實例
我們通過一個實例來學習三層架構。
(1) 以sql2000為例
建立testuser數(shù)據(jù)庫。
表的sql腳本(在查詢分析器中執(zhí)行即可):
/****** object: table [dbo].[customers] script date: 2004-01-08 0:46:35 ******/
create table [dbo].[customers] (
[customerid] [int] identity (1, 1) not null ,
[customername] [char] (20) not null ,
[addr] [varchar] (50) null ,
[city] [char] (20) null ,
[phone] [char] (20) null ,
[fax] [char] (10) null
) on [primary]
go

/****** object: table [dbo].[users] script date: 2004-01-08 0:46:36 ******/
create table [dbo].[users] (
[id] [int] identity (1, 1) not null ,
[truename] [char] (20) not null ,
[regname] [char] (20) not null ,
[pwd] [char] (10) not null ,
[sex] [char] (2) null ,
[email] [char] (20) null
) on [primary]
go

alter table [dbo].[customers] with nocheck add
constraint [pk_customers] primary key nonclustered
(
[customerid]
) on [primary]
go

alter table [dbo].[users] with nocheck add
constraint [pk_users] primary key nonclustered
(
[id]
) on [primary]
go

(2)創(chuàng)建業(yè)務實體層
1.打開vs.net2002,新建一個項目,選asp.net web服務,位置是: http://localhost/mydotnet/tiner/webdata/
2.webservice的代碼
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.diagnostics;
using system.web;
using system.web.services;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace webdata
{
/// <summary>
/// service1 的摘要說明。
/// </summary>
[webservice (namespace = "http://www.ourfly.com", description = "<font size=4 color='#ff6633'><b><br><center>使用c#寫的三層架構的程序。</center></b><br><br></font>")]
public class service1 : system.web.services.webservice
{
sqldataadapter myadapter;
string strconn="data source=localhost;initial catalog=testuser;uid=sa;pwd=";

public service1()
{
//codegen:該調用是 asp.net web 服務設計器所必需的
initializecomponent();
}

#region component designer generated code

//web 服務設計器所必需的
private icontainer components = null;

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if(disposing && components != null)
{
components.dispose();
}
base.dispose(disposing);
}

#endregion

//定義一個私有方法,用來判斷用戶是否存在
private boolean boolreg(string strregname)
{
boolean strresult;
sqlconnection cn;
sqlcommand cmd;

string strsql;
cn=new sqlconnection(strconn);
cn.open();

strsql="select count(*) from users where regname='"+strregname+"'";
cmd=new sqlcommand(strsql,cn);

sqldatareader reader = cmd.executereader();
reader.read();
int i = reader.getint32(0);
if (i>0)
{
reader.close();
cn.close ();
strresult= true;
}
else
{
reader.close();
cn.close ();
strresult=false;
}

return strresult;
}

[webmethod(description="完成用戶注冊功能.")]
public string reguser(string strtruename,string strregname,string strpwd,string strsex,string stremail)
{
string strresult;
sqlconnection cn;
sqlcommand cmd;

//判斷用戶是否存在
if (boolreg(strregname))
{
strresult="這個用戶已經存在,請重新注冊";
return strresult;
}
else
{
string strsql;
cn=new sqlconnection(strconn);
cn.open();

strsql="insert into users (truename,regname,pwd,sex,email) values( '";
strsql+=strtruename+"','";
strsql+=strregname+"','";
strsql+=strpwd+"','";
strsql+=strsex+"','";
strsql+=stremail+"')";

cmd=new sqlcommand(strsql,cn);

try
{
cmd.executenonquery();
cn.close ();
strresult= "用戶注冊成功";
}
catch(exception e)
{
cn.close ();
strresult="請仔細檢查你的輸入項";
}
}
return strresult;

}

[webmethod(description="用戶登錄")]
public string login(string strregname,string strpwd)
{
sqlconnection cn;
sqldataadapter da;
dataset ds;
string strsql,strresult;

strsql="select truename,regname,pwd from users where regname='"+strregname+"' and pwd='"+strpwd+"'";

cn=new sqlconnection(strconn);
cn.open();

da=new sqldataadapter(strsql,cn);
ds=new dataset();
da.fill(ds,"users");

if(ds.tables["users"].rows.count>0)
{
strresult= "登錄成功";

}
else
{
strresult= "用戶名或口令有誤或者沒有這個用戶!請重新輸入!";

}
cn.close();
return strresult;
}


[webmethod(description="得到數(shù)據(jù)集.")]
public dataset getdataset()
{
sqlconnection cn;
cn=new sqlconnection(strconn);
string strsel="select * from customers";
cn.open();
myadapter=new sqldataadapter(strsel,strconn);
dataset ds=new dataset();
myadapter.fill(ds,"customers");
return ds;
}
}
}

運行后如下圖所示:

(3)web表現(xiàn)層
打開vs.net2002,新建一個項目,選asp.net web應用程序,位置是: http://localhost/mydotnet/tiner/webapplication1
在解決方案資源管理器中,右鍵點擊”引用”,選擇”添加web引用”, 輸入http://localhost/mydotnet/tiner/webdata/service1.asmx如下圖所示:

添加引用后,如下圖:
好了,我們開始寫代碼,詳細代碼如下:
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;

namespace tiner
{
/// <summary>
/// webform1 的摘要說明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.label label1;
protected system.web.ui.webcontrols.datagrid datagrid1;
protected system.web.ui.webcontrols.label label2;
protected system.web.ui.webcontrols.label label3;
protected system.web.ui.webcontrols.textbox txtusername;
protected system.web.ui.webcontrols.button btlogin;
protected system.web.ui.webcontrols.button btreg;
protected system.web.ui.webcontrols.panel panel1;
protected system.web.ui.webcontrols.label label4;
protected system.web.ui.webcontrols.label label5;
protected system.web.ui.webcontrols.textbox txttruename;
protected system.web.ui.webcontrols.label label6;
protected system.web.ui.webcontrols.label label7;
protected system.web.ui.webcontrols.label label8;
protected system.web.ui.webcontrols.button btok;
protected system.web.ui.webcontrols.textbox txtregname;
protected system.web.ui.webcontrols.textbox txtpwd;
protected system.web.ui.webcontrols.dropdownlist dropdownlistsex;
protected system.web.ui.webcontrols.textbox txtemail;
protected system.web.ui.webcontrols.textbox txtpassword;

string myresult;
dataset ds;
localhost.service1 myservice =new localhost.service1();

private void page_load(object sender, system.eventargs e)
{
// 在此處放置用戶代碼以初始化頁面
if ( !page.ispostback )
{
panel1.visible =false;
}
}

#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:該調用是 asp.net web 窗體設計器所必需的。
//
initializecomponent();
base.oninit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.btlogin.click += new system.eventhandler(this.btlogin_click);
this.btreg.click += new system.eventhandler(this.btreg_click);
this.btok.click += new system.eventhandler(this.btok_click);
this.load += new system.eventhandler(this.page_load);

}
#endregion



private void btreg_click(object sender, system.eventargs e)
{
datagrid1.visible =false;
panel1.visible =true;
}

private void btlogin_click(object sender, system.eventargs e)
{
if (txtusername.text =="" || txtpassword.text=="")
{
label1.text ="請輸入用戶名或者密碼";
return;
}

datagrid1.visible =true;
panel1.visible =false;
myresult=myservice.login(txtusername.text,txtpassword.text ) ;
if (myresult.tostring() =="登錄成功")
{
ds=myservice.getdataset();
datagrid1.datasource =ds.tables["customers"];
datagrid1.databind();
}
else
{
label1.text ="用戶名或口令有誤或者沒有這個用戶!請重新輸入!";
}
}

private void btok_click(object sender, system.eventargs e)
{
myresult=myservice.reguser(txttruename.text,txtregname.text,txtpwd.text,dropdownlistsex.selecteditem.text ,txtemail.text);
if(myresult.tostring()=="用戶注冊成功" )
{
label1.text ="用戶注冊成功,可以登錄查看信息";
return;
}
else if(myresult.tostring()=="這個用戶已經存在,請重新注冊" )
{
label1.text ="這個用戶已經存在,請重新注冊";
return;
}
else
{
label1.text ="用戶注冊發(fā)生錯誤,請檢查每一項";
return;
}

}

}
}
運行啟動,輸入正確的用戶名和密碼,點擊”登錄”按鈕,會看到下面的界面:
點擊”注冊新用戶”,出現(xiàn)注冊界面,如果注冊的用戶存在,會產生提示:
總結:
web表示層上完全沒有數(shù)據(jù)庫連接操作,它與數(shù)據(jù)庫的連接任務是通過業(yè)務層來完成的,這樣,程序的結構更加清晰。當然,程序中可以增加其它的層,如:業(yè)務規(guī)則層等。
如果錯誤,歡迎大家指教。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 贺州市| 高淳县| 靖宇县| 塘沽区| 马尔康县| 穆棱市| 杨浦区| 台前县| 海安县| 沙洋县| 仙居县| 平阴县| 巫山县| 威远县| 汉寿县| 钟山县| 余江县| 黄浦区| 瓮安县| 内乡县| 扎鲁特旗| 中宁县| 贵阳市| 抚州市| 大埔县| 达孜县| 永善县| 博白县| 多伦县| 横峰县| 耒阳市| 商丘市| 上饶县| 卓资县| 镇平县| 福贡县| 司法| 石渠县| 晋城| 根河市| 孟村|