在C#中实现3层架构(4)

public DACustomer(BOCustomer cus)

{

// A reference of the business object class

}

//standard dataset function that adds a new customer

public void Add(BOCustomer cus)

{

String str = BuildAddString(cus);

OpenCnn();

//Open command option - cnn parameter is imporant

OleDbCommand cmd = new OleDbCommand(str,cnn);

//execute connection

cmd.ExecuteNonQuery();

// close connection

CloseCnn();

}

//standard dataset function that updates

//details of a customer based on ID

public void Update(BOCustomer cus)

{

OpenCnn();

String selectStr = "UPDATE " + thisTable +

" set " + cus_LName + " = '" + cus.LName + "'" +

", " + cus_FName + " = '" + cus.FName + "'" +

", " + cus_Address + " = '" + cus.Address + "'" +

", " + cus_Tel + " = '" + cus.Tel + "'" +

" where cus_ID = '" + cus.cusID + "'";

OleDbCommand cmd = new OleDbCommand(selectStr,cnn);

cmd.ExecuteNonQuery();   

CloseCnn();

}

//standard dataset function that finds and

//return the detail of a customer in a dataset

public DataSet Find(String argStr)

{

DataSet ds=null;

try

{

OpenCnn();     

String selectStr = "select * from " + thisTable +

" where cus_ID = '" + argStr + "'";

OleDbDataAdapter da =

new OleDbDataAdapter(selectStr,cnn);

ds = new DataSet();

da.Fill(ds,thisTable);

CloseCnn();             

}

catch(Exception e)

{

String Str = e.Message;

}

return ds;

}

private void OpenCnn()

{

// initialise connection

String cnnStr = CnnStr;

cnn = new OleDbConnection(cnnStr);

// open connection

cnn.Open();

}

private void CloseCnn()

{

// 5- step five

cnn.Close();

}      

// just a supporting function that builds

// and return the insert string for dataset.

private String BuildAddString(BOCustomer cus)

{

// these are the constants as

// set in the top of this module.

strTable="Insert into " + thisTable;

strFields=" (" + cus_ID +

"," + cus_LName +

"," + cus_FName +

"," + cus_Address +

"," + cus_Tel + ")";          

//these are the attributes of the

//customer business object.

strValues= " Values ( '" + cus.cusID +

"' , '" + cus.LName +

"' , '" + cus.FName +

"' , '" + cus.Address +

"' , '" + cus.Tel + "' )";

insertStr = strTable + strFields + strValues;         

return insertStr;         

}

}

}
posted @ 2009-03-27 10:06  yongbin621  阅读(277)  评论(0编辑  收藏  举报