.net 三层架构 为什么dal中方法不加static

不加的话,在bll层中调用的时候还要写上一句 bll a1=new bll()
2025-04-30 08:21:30
推荐回答(4个)
回答1:

static写多了,内存会吃紧的,亲。
而new虽然麻烦点,但是.net 和 java都有垃圾回收机制。
另外从 类的初始化过程来看,static写多了,启动过程会比较长。
昨天没睡好,写的模糊点,有说的不对的地方见谅。

回答2:

静态方法不利于多线程调用,会有冲突

回答3:

这也不是很麻烦啊。你可以在你要调用的页面写个成员变量,像上面那样,后面要用就只需要
this.a1.xxx

回答4:

usingSystem.Data.SqlClient;usingSystem.Text;publicintAdd(Model.Usermodel){StringBuilderstrSql=newStringBuilder();strSql.Append("insertintoUser(");strSql.Append("Name,pwd,Email");strSql.Append(")");strSql.Append("values(");strSql.Append("'"+model.TxtName+"',");strSql.Append("'"+model.txtPwd+"',");strSql.Append("'"+model.txtEmail+"'");strSql.Append(")");returnSQLHelper.getInt(strSql.ToString());}SQLHelper.cs页面usingSystem.Data.SqlClient;publicclassSQLHelper{protectedstaticSqlConnectionconnection=newSqlConnection("server=.;database=db;integratedsecurity=true");publicintgetInt(stringsql){try{if(connection.State==ConnectionState.Closed){connection.Open();}SqlCommandcommand=newSqlCommand(sql,connection);intInt=Convert.ToInt32(command.ExecuteNonQuery());if(connection.State==ConnectionState.Open){connection.Close();}returnInt;}finally{if(connection.State==ConnectionState.Open){connection.Close();}}}}