细说C#中连接字符串的方法“+”和Append

    C#中连接字符串的方法,通常有以下几种:

(方法1)“+”

     很简单,利用+符号可以将两个字符串连接起来,例如, string sqlstr = @"select * from UserInfo where userName='"+ userName+ "' and password='"+ password+ "'";

(方法2) Append(使用StringBuilder类) //引入命名空间是:using System.Text;

     利用Append也是可以将字符串连接在一起的,如:

   StringBuilder strSql=new StringBuilder();
   strSql.Append("select count(1) from TB_AnnualProductionPlan");
   strSql.Append(" where Id="+Id+" ");
   return DBCommonOper.DBHelp.Exists(strSql.ToString());

   等价于:select count(1) from TB_AnnualProductionPlanwhere Id="+Id+"

               【另外示例】

      public void deleteValue(int ID)
        {
            string strSql = "Data Source=VQJREZV7DVSK2QA;Initial Catalog=gridviewAPP;User ID=sa;Password=admin@123456";
            SqlConnection connew = new SqlConnection(strSql);

             connew.Open();
            StringBuilder strDeletet = new StringBuilder();
            strDeletet.Append("delete from userInfo ");
            strDeletet.Append(" where Id=" + ID + "");
            SqlCommand cmd = new SqlCommand(strDeletet.ToString(),connew);  

             //注意,如果要使用SqlCommand,将stringBuilder属性转换为字符串string类型
            cmd.ExecuteNonQuery();

            connew.Close();

        }

 

(方法3)string sqlReset="update UserInfo setuserName='{0}',password='{1}',QQ='{2}',Phone='{3}',Email='{4}',Address='{5}' where ID='{6}'";
                sqlReset = string.Format(sqlReset, txtUser.Text.Trim(), txtPassword.Text.Trim(), txtQQ.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), txtAddress.Text.Trim(),Convert.ToInt32(this.dgvUser.CurrentRow.Cells[0].Value.ToString()));

(方法4)  参数法,请详见后面章节【SqlParameter的应用

string sqlReset = "update UserInfo set userName=@userName,password=@password,QQ=@QQ,Phone=@Phone,Email=@Email,Address=@Address whereID=@ID";
                    SqlParameter sp = new SqlParameter("@userName", txtUser.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@password", txtPassword.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@QQ", txtQQ.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@Phone", txtPhone.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@Email", txtEmail.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@Address", txtAddress.Text.Trim());
                    cmd.Parameters.Add(sp);
                    sp = new SqlParameter("@ID", Convert.ToInt32(this.dgvUser.CurrentRow.Cells[0].Value.ToString()));
                    cmd.Parameters.Add(sp);

 

(3)两者的区别:

   两者功能都是一样:连接字符串。两者之间的区别在于执行效率上面的问题。

   Append构建字符串的效率比使用+连接的高,如果有较多的字符串需要拼接,建议使用append进行拼接;少的话,使用用+更方便阅读。

 
(4)C#中也可以使用 String.Concat 函数来连接字符串。但是这不光是耗费内存,还耗费cpu执行的时间。不建议使用。
(5)大段文本转为StringBuilder,用来拼接字符串,效率提高。如下所示:(此方法例子源自EasyCode软件例子)


以下内容为测试文本

在软件的设计开发过程中,
经常会需要将大段文本定义到程序中时,
可以考虑将字符串放在资源文件“.resx”中去,

但是如果字符串需要进行转换,
比如进行Replace替换,
那么所需要内存和耗费的时间,
可能多的超出您的想像。

如果您对性能有较高要求,
那么在您的程序中尽可能的不要使用Replace,
使用本工具来帮您生成StringBuilder来拼接字符串吧

经过大量的测试,
我们发现采用StringBuilder来处理大段文本,
性能至少要比使用Resx文件快一倍。

快试试看吧。

                        ——BudStudio 爱英思躺

》》》》》》》》》》》》》》》》》》》》》》》以下是拼凑方法:

StringBuilder contentStr = new StringBuilder();
contentStr.AppendLine();
contentStr.AppendLine("以下内容为测试文本");
contentStr.AppendLine();
contentStr.AppendLine("在软件的设计开发过程中,");
contentStr.AppendLine("经常会需要将大段文本定义到程序中时,");
contentStr.AppendLine("可以考虑将字符串放在资源文件“.resx”中去,");
contentStr.AppendLine();
contentStr.AppendLine("但是如果字符串需要进行转换,");
contentStr.AppendLine("比如进行Replace替换,");
contentStr.AppendLine("那么所需要内存和耗费的时间,");
contentStr.AppendLine("可能多的超出您的想像。");
contentStr.AppendLine();
contentStr.AppendLine("如果您对性能有较高要求,");
contentStr.AppendLine("那么在您的程序中尽可能的不要使用Replace,");
contentStr.AppendLine("使用本工具来帮您生成StringBuilder来拼接字符串吧。");
contentStr.AppendLine();
contentStr.AppendLine("经过大量的测试,");
contentStr.AppendLine("我们发现采用StringBuilder来处理大段文本,");
contentStr.AppendLine("性能至少要比使用Resx文件快一倍。");
contentStr.AppendLine();
contentStr.AppendLine("快试试看吧。");
contentStr.AppendLine();
contentStr.AppendLine("                        ——BudStudio 爱英思躺");

%%%%%注意:Append和AppendLine的区别
AppendLine会在结果在控制台输出后,光标移到了结果的下2行。意味着输出结果后就空行了。而Append不会。
 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值