Is divided into four layers, and a presentation layer (UI), business logic layer (BLL), data access layer (DAL), the model layer is the entity class, you first create a blank project, and then build the UI layer, choose web, empty MVC, under construction in other layers are selected class library, built after the application is relationship, citing the BLL and UI layer model layer, and then the BLL reference DAL layer and model layer, and then the DAL reference model layer, model layer is to be referenced, and is created in the DAL DBHelper class, this class is used to write to add and delete methods, wrote this class is convenient many, every time want to use it to call directly, reduce a lot of unnecessary resource, write a case:
First:
//connection data string
The static string sqlcon="server=. Uid=sa; The PWD=password; The database=database name ";
//create a connection object
Public static sqlconnection GetCon ()
{
Return new sqlconnection (sqlcon);
}
Deletion method, the affected function (gm)
Public static int GetNonQuery (string SQL, commandType type, params sqlParameter [] par)
{
Sqlconnection con=GetCon ();
int count=0;
Try {
Con. The Open ();
SqlCommand com=new sqlCommand (SQL, con);
Com.com mandType=type;
If (par) length> 0 & amp; & Par!=null)
{
Com. The parameters. AddRange (par);
}
Count=(int) com. ExecuteNonQuery ();
} the catch (Exception)
{
Throw;
} the finally
{
//close resources
Con. The close ();
}
Return the count.
}
The code above is DBHelper class one of the methods, increased when use it, just write this one, is returned by the query method is a reader, and then is written to the data access layer method, write the same to the BLL call again, said to the UI layer,
Above is about such a process, to update the , brother, we also just learned about this,
CodePudding user response: