2023年6月21日发(作者:)

细说C#中连接字符串的⽅法“+”和Append C#中连接字符串的⽅法,通常有以下⼏种:(⽅法1)“+” 很简单,利⽤+符号可以将两个字符串连接起来,例如, string sqlstr = @"select * from UserInfo where userName='"+userName+ "' and password='"+ password+ "'";(⽅法2) Append(使⽤StringBuilder类) //引⼊命名空间是:using ; 利⽤Append也是可以将字符串连接在⼀起的,如: StringBuilder strSql=new StringBuilder(); ("select count(1) from TB_AnnualProductionPlan"); (" where Id="+Id+" "); return (ng()); 等价于:select count(1) from TB_AnnualProductionPlanwhere Id="+Id+"

【另外⽰例】 public void deleteValue(int ID) { string strSql = "Data Source=VQJREZV7DVSK2QA;Initial Catalog=gridviewAPP;UserID=sa;Password=admin@123456"; SqlConnection connew = new SqlConnection(strSql); (); StringBuilder strDeletet = new StringBuilder(); ("delete from userInfo "); (" where Id=" + ID + ""); SqlCommand cmd = new SqlCommand(ng(),connew);

//注意,如果要使⽤SqlCommand,将stringBuilder属性转换为字符串string类型 eNonQuery(); (); }

(⽅法3)string sqlReset="update UserInfo setuserName='{0}',password='{1}',QQ='{2}',Phone='{3}',Email='{4}',Address='{5}'where ID='{6}'"; sqlReset = (sqlReset, (), (), (),(), (),(),32([0].ng()));(⽅法4) 参数法,请详见后⾯章节【】string sqlReset = "update UserInfo set where"; SqlParameter sp = new SqlParameter("@userName", ()); (sp); sp = new SqlParameter("@password", ()); (sp); sp = new SqlParameter("@QQ", ()); (sp); sp = new SqlParameter("@Phone", ()); (sp); sp = new SqlParameter("@Email", ()); (sp); sp = new SqlParameter("@Address", ()); (sp); sp = new SqlParameter("@ID", 32([0].ng())); (sp);

(3)两者的区别: 两者功能都是⼀样:连接字符串。两者之间的区别在于执⾏效率上⾯的问题。 Append构建字符串的效率⽐使⽤+连接的⾼,如果有较多的字符串需要拼接,建议使⽤append进⾏拼接;少的话,使⽤⽤+更⽅便阅读。

(4)C#中也可以使⽤ 函数来连接字符串。但是这不光是耗费内存,还耗费cpu执⾏的时间。不建议使⽤。

(5)⼤段⽂本转为StringBuilder,⽤来拼接字符串,效率提⾼。如下所⽰:(此⽅法例⼦源⾃EasyCode软件例⼦)以下内容为测试⽂本在软件的设计开发过程中,经常会需要将⼤段⽂本定义到程序中时,可以考虑将字符串放在资源⽂件“.resx”中去,但是如果字符串需要进⾏转换,⽐如进⾏Replace替换,那么所需要内存和耗费的时间,可能多的超出您的想像。如果您对性能有较⾼要求,那么在您的程序中尽可能的不要使⽤Replace,使⽤本⼯具来帮您⽣成StringBuilder来拼接字符串吧。经过⼤量的测试,我们发现采⽤StringBuilder来处理⼤段⽂本,性能⾄少要⽐使⽤Resx⽂件快⼀倍。快试试看吧。 ——BudStudio 爱英思躺》》》》》》》》》》》》》》》》》》》》》》》以下是拼凑⽅法:StringBuilder contentStr = new StringBuilder();Line();Line("以下内容为测试⽂本");Line();Line("在软件的设计开发过程中,");Line("经常会需要将⼤段⽂本定义到程序中时,");Line("可以考虑将字符串放在资源⽂件“.resx”中去,");Line();Line("但是如果字符串需要进⾏转换,");Line("⽐如进⾏Replace替换,");Line("那么所需要内存和耗费的时间,");Line("可能多的超出您的想像。");Line();Line("如果您对性能有较⾼要求,");Line("那么在您的程序中尽可能的不要使⽤Replace,");Line("使⽤本⼯具来帮您⽣成StringBuilder来拼接字符串吧。");Line();Line("经过⼤量的测试,");Line("我们发现采⽤StringBuilder来处理⼤段⽂本,");Line("性能⾄少要⽐使⽤Resx⽂件快⼀倍。");Line();Line("快试试看吧。");Line();Line(" ——BudStudio 爱英思躺");%%%%%注意:Append和AppendLine的区别AppendLine会在结果在控制台输出后,光标移到了结果的下2⾏。意味着输出结果后就空⾏了。⽽Append不会。

2023年6月21日发(作者:)

细说C#中连接字符串的⽅法“+”和Append C#中连接字符串的⽅法,通常有以下⼏种:(⽅法1)“+” 很简单,利⽤+符号可以将两个字符串连接起来,例如, string sqlstr = @"select * from UserInfo where userName='"+userName+ "' and password='"+ password+ "'";(⽅法2) Append(使⽤StringBuilder类) //引⼊命名空间是:using ; 利⽤Append也是可以将字符串连接在⼀起的,如: StringBuilder strSql=new StringBuilder(); ("select count(1) from TB_AnnualProductionPlan"); (" where Id="+Id+" "); return (ng()); 等价于:select count(1) from TB_AnnualProductionPlanwhere Id="+Id+"

【另外⽰例】 public void deleteValue(int ID) { string strSql = "Data Source=VQJREZV7DVSK2QA;Initial Catalog=gridviewAPP;UserID=sa;Password=admin@123456"; SqlConnection connew = new SqlConnection(strSql); (); StringBuilder strDeletet = new StringBuilder(); ("delete from userInfo "); (" where Id=" + ID + ""); SqlCommand cmd = new SqlCommand(ng(),connew);

//注意,如果要使⽤SqlCommand,将stringBuilder属性转换为字符串string类型 eNonQuery(); (); }

(⽅法3)string sqlReset="update UserInfo setuserName='{0}',password='{1}',QQ='{2}',Phone='{3}',Email='{4}',Address='{5}'where ID='{6}'"; sqlReset = (sqlReset, (), (), (),(), (),(),32([0].ng()));(⽅法4) 参数法,请详见后⾯章节【】string sqlReset = "update UserInfo set where"; SqlParameter sp = new SqlParameter("@userName", ()); (sp); sp = new SqlParameter("@password", ()); (sp); sp = new SqlParameter("@QQ", ()); (sp); sp = new SqlParameter("@Phone", ()); (sp); sp = new SqlParameter("@Email", ()); (sp); sp = new SqlParameter("@Address", ()); (sp); sp = new SqlParameter("@ID", 32([0].ng())); (sp);

(3)两者的区别: 两者功能都是⼀样:连接字符串。两者之间的区别在于执⾏效率上⾯的问题。 Append构建字符串的效率⽐使⽤+连接的⾼,如果有较多的字符串需要拼接,建议使⽤append进⾏拼接;少的话,使⽤⽤+更⽅便阅读。

(4)C#中也可以使⽤ 函数来连接字符串。但是这不光是耗费内存,还耗费cpu执⾏的时间。不建议使⽤。

(5)⼤段⽂本转为StringBuilder,⽤来拼接字符串,效率提⾼。如下所⽰:(此⽅法例⼦源⾃EasyCode软件例⼦)以下内容为测试⽂本在软件的设计开发过程中,经常会需要将⼤段⽂本定义到程序中时,可以考虑将字符串放在资源⽂件“.resx”中去,但是如果字符串需要进⾏转换,⽐如进⾏Replace替换,那么所需要内存和耗费的时间,可能多的超出您的想像。如果您对性能有较⾼要求,那么在您的程序中尽可能的不要使⽤Replace,使⽤本⼯具来帮您⽣成StringBuilder来拼接字符串吧。经过⼤量的测试,我们发现采⽤StringBuilder来处理⼤段⽂本,性能⾄少要⽐使⽤Resx⽂件快⼀倍。快试试看吧。 ——BudStudio 爱英思躺》》》》》》》》》》》》》》》》》》》》》》》以下是拼凑⽅法:StringBuilder contentStr = new StringBuilder();Line();Line("以下内容为测试⽂本");Line();Line("在软件的设计开发过程中,");Line("经常会需要将⼤段⽂本定义到程序中时,");Line("可以考虑将字符串放在资源⽂件“.resx”中去,");Line();Line("但是如果字符串需要进⾏转换,");Line("⽐如进⾏Replace替换,");Line("那么所需要内存和耗费的时间,");Line("可能多的超出您的想像。");Line();Line("如果您对性能有较⾼要求,");Line("那么在您的程序中尽可能的不要使⽤Replace,");Line("使⽤本⼯具来帮您⽣成StringBuilder来拼接字符串吧。");Line();Line("经过⼤量的测试,");Line("我们发现采⽤StringBuilder来处理⼤段⽂本,");Line("性能⾄少要⽐使⽤Resx⽂件快⼀倍。");Line();Line("快试试看吧。");Line();Line(" ——BudStudio 爱英思躺");%%%%%注意:Append和AppendLine的区别AppendLine会在结果在控制台输出后,光标移到了结果的下2⾏。意味着输出结果后就空⾏了。⽽Append不会。