C#中定义接口:
public interface Sk_usersCL
{
int Uid { get;}
string UserName { set;get;}
string PassWord { set;get;}
string Sex { set;get;}
string Connectionstring { set;get;}
int InsusersCL();
int GetdatausersCl();
}
实现接口的类:
public class sk_usersclsCL : Sk_usersCL
{
private int puid;
private string pusername;
private string ppassword;
private string psex;
private string pconnectionstring;
private SqlConnection conn;
public int Uid
{
get { return puid; }
}
public string UserName
{
set { pusername = value; }
get { return pusername; }
}
public string PassWord
{
set { ppassword = value; }
get { return ppassword; }
}
public string Sex
{
set { psex = value; }
get { return psex; }
}
public string Connectionstring
{
set { pconnectionstring = value; }
get { return pconnectionstring; }
}
public void Open()
{
conn = new SqlConnection(pconnectionstring);
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
}
public void Close()
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
public int InsusersCL()
{
Open();
string strinssql = "insert into users (user_name,pwd,sex) values('"+UserName+"','"+PassWord+"','"+Sex+"')";
SqlCommand cmd = new SqlCommand(strinssql, conn);
int intnum = cmd.ExecuteNonQuery();
Close();
return intnum;
}
public int UpdusersCL()
{
Open();
string strupdsql = "update users set pwd='"+PassWord+"' where user_name='"+UserName+"'";
SqlCommand cmd = new SqlCommand(strupdsql,conn);
int rcount = cmd.ExecuteNonQuery();
Close();
return rcount;
}
创建一个逻辑层:
//创建接口
Sk_usersCL.Sk_usersCL userCL;
#region 定义属性,
private int puid;
private string pusername;
private string ppassword;
private string psex;
private string pconnstr;
public int Uid
{
get { return puid; }
set { puid = value; }
}
public string UserName
{
get { return pusername; }
set { pusername = value; }
}
public string PassWord
{
get { return ppassword; }
set { ppassword = value; }
}
public string Sex
{
get { return psex; }
set { psex = value; }
}
public string Connectionstring
{
get { return pconnstr; }
set { pconnstr = value; }
}
#endregion
public string GetConnstr()
{
return "server=Data-11;User id=11; Pwd=11;database=11";
}
public int insusers()
{
sk_usersclsCL users = new sk_usersclsCL();
userCL = users;
userCL.UserName = pusername;
userCL.PassWord = ppassword;
userCL.Sex = psex;
userCL.Connectionstring = GetConnstr();
return userCL.InsusersCL();
}
public int Updusers()
{
sk_usersclsCL users = new sk_usersclsCL();//创建数据层的对象
userCL = users; //赋值给接口对象,接口调用继承该接口的类
userCL.UserName = pusername;
userCL.PassWord = ppassword;
userCL.Sex = psex;
userCL.Connectionstring = GetConnstr();
return userCL.UpdusersCL();
}
}
表现层调用逻辑层:
Sk_usersBL.Class1 us = new Sk_usersBL.Class1(); //声明逻辑层的对象
us.UserName = TBusername.Text;
us.PassWord = TBpassword.Text;
if (us.Updusers() > 0)
{
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}