日期:2014-05-20  浏览次数:20865 次

效率问题.
2个方法那个好.   好在那里.为什么好.  

protected   void   RunCommand(string   commandString,out   int   rowsAffected)
{
if(this.Connection==null)
this.Connection=new   SqlConnection(this.connectionString);
if(this.Connection.State!=System.Data.ConnectionState.Open)
Connection.Open();
SqlCommand   command   =   BuildCommand(   commandString   );
rowsAffected   =   command.ExecuteNonQuery();
Connection.Close();
this.Connection.Dispose();
}

protected   int   RunCommand(string   commandString)
{
                  int   rowsAffected;
if(this.Connection==null)
this.Connection=new   SqlConnection(this.connectionString);
if(this.Connection.State!=System.Data.ConnectionState.Open)
Connection.Open();
SqlCommand   command   =   BuildCommand(   commandString   );
rowsAffected   =   command.ExecuteNonQuery();
Connection.Close();
this.Connection.Dispose();
                  return   rowsAffected   ;  
}




------解决方案--------------------
2
------解决方案--------------------
2 少申请了一个变量 也许还会少一次赋值过程

------解决方案--------------------
个人认为一样,只是处理方式不同,第一种是用输出参数out,相当于传引用,第二个是返回一个int值,貌似这两种效率上的比较实在体现不出来....从习惯上看觉得第二种的思维方式好一点吧
楼下补充~
------解决方案--------------------
效率差别应该不大
------解决方案--------------------
如果性能出现瓶颈 效率跟不上的话 问题肯定不会出在这样的类似的地方
多半是数据库操作 或者New一个对象什么的 或者是rount trip太多等
个人以为通过这个改变性能意义不大(几乎没有) 呵呵