Give you an example [. NET three-tier framework development example-user registration system]

Source: Internet
Author: User
Tags insert
Database base class
Using System;
Using System.Data;
Using System.Data.SqlClient;

Namespace Haisky.htjob
{
<summary>
Summary description of the Dbclass.
</summary>
public class Dbclass
{
private string connectionString;
protected SqlConnection Connection;
Public Dbclass (String newconnectionstring)
{
connectionString = newconnectionstring;
Connection = new SqlConnection (connectionString);
}
public string ConnectionString
{
Get
{
return connectionString;
}
}
Private SqlCommand Buildquerycommand (string storedprocname,idataparameter[] parameters)
{
SqlCommand command = new SqlCommand (storedprocname,connection);
Command.commandtype = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
return command;
}
Private SqlCommand Buildintcommand (string storedprocname,idataparameter[] parameters)
{
SqlCommand command = Buildquerycommand (storedprocname,parameters);
Command. Parameters.Add (New SqlParameter ("ReturnValue", sqldbtype.int,4,
Parameterdirection.returnvalue,false,
0,
0,
String. Empty,datarowversion.default,null));
return command;
}
protected int Runprocedure (string storedprocname,idataparameter[] parameters,out int rowsaffected)
{
int result;
Connection.Open ();
SqlCommand command = Buildintcommand (storedprocname,parameters);
rowsaffected = command. ExecuteNonQuery ();
result = (int) command. parameters["ReturnValue"]. Value;
Connection.close ();
return result;
}
Protected SqlDataReader runprocedure (String storedprocname, idataparameter[] parameters)
{
SqlDataReader Returnreader;

Connection.Open ();
SqlCommand command = Buildquerycommand (storedprocname, parameters);
Command.commandtype = CommandType.StoredProcedure;

Returnreader = command. ExecuteReader ();
return returnreader;
}
Protected DataSet runprocedure (String storedprocname, idataparameter[] parameters, string tablename)
{
DataSet DataSet = new DataSet ();
Connection.Open ();
SqlDataAdapter SqlDA = new SqlDataAdapter ();
Sqlda.selectcommand = Buildquerycommand (storedprocname, parameters);
Sqlda.fill (DataSet, TableName);
Connection.close ();

return dataSet;
}
protected void Runprocedure (String storedprocname, idataparameter[] Parameters, DataSet DataSet, string tablename)
{
Connection.Open ();
SqlDataAdapter SqlDA = new SqlDataAdapter ();
Sqlda.selectcommand = Buildintcommand (storedprocname, parameters);
Sqlda.fill (DataSet, TableName);
Connection.close ();
}
}
}

Business layer base class (logical layer)
Using System;
Namespace Wrox.WebModules.Business
{
public class Bizobject
{
Public Bizobject ()
{            }
}
}

The following is an example of a three-tier architecture developed by the user registration system: The database base class uses the code provided above.
1, user registration module data layer development:
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using Haisky.htjob;
Using System.Xml;
Namespace HaiSky.HtJob.Accounts.AccountsData
{
public class User:HaiSky.HtJob.HaiSkyDbObject
{
Public User (String newconnectionstring): Base (newconnectionstring)
{//Direct routing connection string}
public int Create (string user_nm,string user_pwd)
{
int rowsaffected;
sqlparameter[] Parameters = {New SqlParameter ("@user_nm", sqldbtype.char,16),
New
SqlParameter ("@user_pwd", sqldbtype.char,16)};
Parameters[0]. Value = user_nm;
PARAMETERS[1]. Value = user_pwd;
PARAMETERS[2]. Direction = ParameterDirection.Output;
Try
{
Runprocedure ("If_user_info", Parameters,out rowsaffected);
}
Catch
{    }
return (int) parameters[2]. Value;
}
}
}
2, the user registers the business layer development:
Using System;
Using System.Configuration;
Using HaiSky.HtJob.Accounts.AccountsData;
Namespace HaiSky.HtJob.Accounts.AccountBusiness
{
public class User:HaiSky.HtJob.HaiSkyBizObject
{
int UserID;
String UserName;
String userpwd;
String strconn;
Public User ()
{
strconn = configurationsettings.appsettings["strconn"];
}
public int careate ()
{
Accountsdata.user datauser = new Accountsdata.user (strconn);
UserID = Datauser.create (USERNAME,USERPWD);
return UserID;
}
public int UserID
{
Get
{
return UserID;
}
Set
{
UserID = value;
}
}
public string UserName
{
Get
{
return userName;
}
Set
{
UserName = value;
}
}
public string Userpwd
{
Get
{
return userpwd;
}
Set
{
Userpwd = value;
}
}
}
}
3, the user registers the presentation layer development:
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 HaiSky.HtJob.Accounts.AccountBusiness;

Namespace HaiSky.HtJob.Modules.Accounts
{
public class Register:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load (object sender, System.EventArgs e)
{
Table1.rows[0]. Cells[0]. Visible = false;
}
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
private void Button1_Click (object sender, System.EventArgs e)
{
int i;
HtJob.Accounts.AccountBusiness.User businessuser = new HtJob.Accounts.AccountBusiness.User ();
Businessuser.username = TextBox1.Text;
Businessuser.userpwd = TextBox2.Text;
i = Businessuser.careate ();
Response.Write (i.ToString ()); Output return value here
}
}
}
4, the system calls the stored procedures:
CREATE PROCEDURE If_user_info
(@user_nm char (), @user_pwd char (), @user_ID int output)
As
Insert User_info (USER_NM,USER_PWD) VALUES (@user_nm, @user_pwd)
Set @user_ID = @ @identity
IF @ @ERROR > 0
BEGIN
RAISERROR (' Insert of Article failed ', 16, 1)
Return 99
End
Go





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.