top
Loading...
基于ASP.NET技術的駕校網頁設計
摘 要 本文以駕校管理系統為例,介紹如何利用asp.netSQL server 2000來進行動態網頁設計,以滿足用戶對數據庫實時更新以及查詢的要求。

關鍵詞 asp.net; c#; SQL server 2000

引言

近年來,隨著Internet的迅速發展以及網頁制作技術的日臻完善,駕校信息管理系統軟件的設計也日趨簡單化和規范化。這里我們將采用asp.net動態網頁技術,通過編寫c#腳本語言對SQL server 2000數據庫進行操作,以實現系統中的諸多功能,如“報名錄入”,“報名資料查詢”,“確認合格學員”,“學員資料查詢”等等。

asp.net 、c#和ado.net

1、ASP.NET是microsoft推出的一種強大的Web服務器端技術,與ASP相比,ASP.NET擁有更高性能的編譯特性與緩存機制。支持多種開發語言,包括C#、J#、Visual Basic和JScript。ASP.NET分離程序代碼與顯示內容,使代碼看起來更簡潔。由于ASP.NET的程序代碼是編譯過的,所以執行時會比ASP快很多。

2、C#語言是Microsoft針對.Net平臺開發的一種全新的編程語言。它是一種面相對象的開發語言,因此具有封裝、繼承和多態性。C#語法簡潔,效率高,并且可以與以其他.NET語言編寫的代碼進行兼容。

3、ADO.NET用于在 Microsoft .NET 平臺中提供數據訪問服務。它作用在服務器端通過執行SQL命令對數據庫進行訪問和更新。ADO.net主要包括Connection,Dataset和Command三個對象, 它們的主要功能如下:

Connection對象:連接數據庫;
Dataset對象:存取數據庫的內容;
Command對象:對數據庫執行查詢指令,以及執行非查詢(更新、刪除和添加等)命令。

網頁設計


下面將以駕校管理系統軟件中的“學員資料查詢與修改”為例說明有關網頁設計問題。(c#)

首先,在SQL server 2000中建一個名為”jx”的數據庫,包含表”車型”、”學員資料” 和視圖”V學員資料”。

其次,”學員資料查詢與修改”主要由三個頁面組成,為:xy_search.aspx、xy_search_win.aspx 、 xy_search_detail.aspx,分別用于輸入查詢條件、顯示查詢結果、顯示其中一條記錄的詳細資料并根據需要進行修改。

1、xy_search.aspx

在該頁面中根據用戶的查詢條件設計頁面,方法:用鼠標左鍵選中工具箱web窗體中的控件,拖拽到頁面,放置到合適的位置,然后設置該控件的屬性,其它的控件類似。如圖1所示。



圖1

頁面設計好要進行后臺代碼的編寫,在xy_search.aspx.cs中進行:


using System.Data.SqlClient;
using System.Data.SqlTypes;// 添加命名空間

public string name1
{
get
{
return
TextBox9.Text.Trim();
}
}

//定義TextBox9中的內容為公用變量,設置文本框的Visible屬性為False

private void Page_Load(object sender, System.EventArgs e)
{
Page.RegisterStartupScript("focus", "<script language= javascript> document.getElementById('TextBox1').focus();</script>");//插入java腳本(聚焦TextBox1)

if(!IsPostBack)// 檢查是否是第一次加載本頁
{
SqlConnection myConnection=new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

myConnection.Open();//連接數據庫
SqlDataAdapter myAdapter=new SqlDataAdapter("select * from 車型",myConnection);// 建立myAdapter對象
DataSet myDataSet=new DataSet(); //建立一個myDataSet對象
myAdapter.Fill(myDataSet,"車型");//把執行select語句得到的記錄添加到myDataSet中 DropDownList1.DataSource=myDataSet.Tables["車型"].DefaultView;
//指定DropDownList1數據源
DropDownList1.DataTextField="車型";
//指定DropDownList1的文本值為“車型”表中‘車型’字段
DropDownList1.DataValueField="車型ID";

//指定DropDownList1的Value值為“車型”表中‘車型ID’字段

DropDownList1.DataBind();//綁定數據
DropDownList1.SelectedValue="C1";//指定DropDownList1的默認Value值為C1
myConnection.Close();//關閉數據庫連接
}
}// 在此處放置用戶代碼以初始化頁面

/*由于在實現刪改功能的同時可以實現查詢功能因此僅以刪改功能為例*/

private void Button2_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 姓名 like '" + TextBox1.Text.Trim() + "'";
//定義姓名模糊查詢條件語句

Server.Transfer("xy_search_win.aspx");

//傳送姓名查詢條件語句到第二個頁面
}

private void Button4_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 證件號碼= '" + TextBox2.Text.Trim() + "'";
//證件號碼= TextBox2.Text的值
Server.Transfer("xy_search_win.aspx");
}

private void Button6_Click(object sender, System.EventArgs e)
{
extBox9.Text = "where 學號= '" + TextBox3.Text.Trim() + "'";
//學號= TextBox3.Text的值
Server.Transfer("xy_search_win.aspx");
}
private void Button8_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 學號 like '"+TextBox4.Text.Trim()+"%'";
//學號與TextBox4.Text的值模糊匹配
Server.Transfer("xy_search_win.aspx");
}

private void Button10_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 報名日期 between '"+ TextBox5.Text.Trim() + "' and '" + TextBox6.Text.Trim() + "'";
//TextBox5.Text>=報名日期<=TextBox6.Text
Server.Transfer("xy_search_win.aspx");
}

private void Button12_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 報考車種 = '"+DropDownList1.SelectedValue.ToString().Trim()+"'";
//報考車種=DropDownList1選中項的value值
Server.Transfer("xy_search_win.aspx");
}

private void Button14_Click(object sender, System.EventArgs e)
{
TextBox9.Text = "where 欠款金額 between "+Convert.ToDecimal(TextBox7.Text.Trim())+" and "+Convert.ToDecimal(TextBox8.Text.Trim())+" ";

//注意要將字符串類型轉換成貨幣類型decimal

Server.Transfer("xy_search_win.aspx");
}

2、xy_search_win.aspx

在該頁面中顯示查詢結果,如圖2所示,此外還可以選中任一條記錄進行刪除和編輯操作。注意:在DataGrid1的”屬性生成器”中添加‘刪除’和‘選擇’列,并將選擇列的文本設成‘編輯’。


圖2

xy_search_win.aspx.cs代碼如下:

using System.Data.SqlClient;//添加命名空間

public class xy_search_win : System.Web.UI.Page
{
public xy_search sourcepage;

//定義第一個頁面為當前頁面的源頁面

public string name1
{
get
{
return DataGrid1.Items[DataGrid1.SelectedIndex].Cells[2].Text.ToString();
}
}//定義所選行的‘學號’為公共變量

private void search1()//定義一個查詢子程序
{
SqlConnection myConnection= new SqlConnection ("server=JXSERVER;uid=sa;pwd=1818;database=jx");

myConnection.Open();//連接數據庫jx
SqlDataAdapter adapter1= new SqlDataAdapter ("select 學號,姓名,報考車種,聯系方式,證件號碼,培訓方式,欠款金額,備注 from V學員資料 "+ TextBox1.Text + " order by 學號", myConnection); //查詢視圖

DataSet myDataSet=new DataSet(); Adapter1.Fill(myDataSet,"result1");
DataGrid1.DataSource=myDataSet.Tables["result1"];
DataGrid1.DataKeyField="學號";//指定關鍵字段為學號
DataGrid1.DataBind();//綁定數據
myConnection.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
sourcepage=(xy_search)Context.Handler;
// 使用Context.Handler屬性來獲得前一個頁面實例對象的引用
TextBox1.Text=sourcepage.name1;//將第一個頁面傳過來的查詢語句賦給TextBox1
search1();//調用查詢子程序顯示查詢結果
}
}

private void DataGrid1_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)//”刪除”按鈕操作代碼
{
SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

myConnection.Open();
string mydelete="delete from 學員資料 where 學號='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";//刪除“學員資料”表中‘學號’等于所選行對應的學號

SqlCommand myCommand=new SqlCommand(mydelete, myConnection);//創建myCommand對象
myCommand.ExecuteNonQuery();//執行sql語句
myConnection.Close();
search1();//重新調用子程序刷新頁面;
}
private void
DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
//”編輯”按鈕命令
{
Server.Transfer("xy_search_detail.aspx");
}

//轉到第三個頁面進行詳細編輯
}

3、xy_search_detail.aspx

在該頁面中可以對學員資料中帶*號的項進行編輯,其它項的文本框的屬性’ReadOnly’設置成true,不可以進行修改,如圖3所示


圖3

using System.Data.SqlClient;

public class xy_search_detail : System.Web.UI.Page
{
public xy_search_win sourcepage;//設置第二個頁面為當前頁面的源頁面
private void myselect1()//定義查詢子程序
{
SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

myConnection.Open();
string myselect1="select 學號,姓名,性別,報考車種,證件號碼,手機,小靈通,住宅電話,辦公電話,備注 from 學員資料 where 學號='"+TextBox10.Text+"'";//查詢視圖,TextBox10.Text為第二個頁面傳遞過來的學號

SqlCommand myCommand1=new SqlCommand(myselect1, myConnection);
SqlDataReader myReader1 =myCommand1.ExecuteReader();//執行讀操作
while(myReader1.Read())//該條記錄不為空時執行{…}里的程序
{
TextBox1.Text=myReader1.GetString(0);//學號賦值給TextBox1
TextBox2.Text=myReader1.GetString(1);//姓名
TextBox3.Text=myReader1.GetString(2);//性別
TextBox4.Text=myReader1.GetString(3);//報考車種
TextBox5.Text=myReader1.GetString(4);//證件號碼
if(myReader1.IsDBNull(5)==false)
{
TextBox9.Text=myReader1.GetString(5);
//手機
}
//判斷該記錄的手機這一列數據是否為空
else {TextBox9.Text=""; }
if(myReader1.IsDBNull(6)==false)
{TextBox11.Text=myReader1.GetString(6);//小靈通 }
else {TextBox11.Text="";}
if(myReader1.IsDBNull(7)==false)
{TextBox12.Text=myReader1.GetString(7);//住宅電話}
else {TextBox12.Text="";}
if(myReader1.IsDBNull(8)==false)
{TextBox13.Text=myReader1.GetString(8);//辦公電話
}
else {TextBox13.Text="";}
if(myReader1.IsDBNull(9)==false)
{TextBox7.Text=myReader1.GetString(9);}//備注
else {TextBox7.Text=""; }
}
myReader1.Close();
myConnection.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
sourcepage=(xy_search_win)Context.Handler;
TextBox10.Text=sourcepage.name1;//將第二個頁面的‘學號’值傳到第三個頁面
Label3.Text="";//提示標簽置空
myselect1();//調用查詢資料子程序
}
}

private void Button1_Click(object sender, System.EventArgs e) //“修改“按鈕命令
{
SqlConnection myConnection=new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

myConnection.Open();
string lxfs=TextBox9.Text.Trim()+"+"+TextBox11.Text.Trim()+"+"+_
TextBox12.Text.Trim()+"+"+TextBox13.Text.Trim()+"+"; //定義聯系方式

string myupdate= "update 學員資料 set _
手機='"+TextBox9.Text.Trim()+"',小靈通='"+TextBox11.Text.Trim()+"',_
住宅電話='"+TextBox12.Text.Trim()+"',辦公電話='"+TextBox13.Text.Trim()+"',_
備注='"+TextBox7.Text+"',聯系方式='"+lxfs+"' where 學號="+TextBox10.Text.Trim()+"";

SqlCommand myCommand2 = new SqlCommand(myupdate,myConnection);
MyCommand2.ExecuteNonQuery();//
Label3.Text="修改成功,請繼續!";
myConnection.Close();
}

private void Button2_Click(object sender, System.EventArgs e)//“重置”按鈕命令
{
Label3.Text="";//提示標簽置空
myselect1();//調用查詢資料子程序
}

}

結束語

本文簡單介紹利用了如何利用VisualStudio.NET2003開發ASP.NETWeb應用程序。開發出來的該系統具有的查詢快捷、存儲量大、可靠性高等特點,有效地提高了駕校管理系統的效率。
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗