saloj
asked on
StringBuilder.AppendFormat
Hi EE,
how can I write the following code in StringBuilder.AppendFormat .
what is the advantage to use Stringbuilder.AppendFormat insted of string concatenation.
SQL = "INSERT INTO tblLog"
SQL += "(ACCOUNT,IP_ADDRESS,USER_ AGENT,REFE RER,LOGIN_ DATE,LOGIN _RESULT_TY PE)"
SQL += " values"
SQL += "(N'" & LCase(Trim(UserAccount)) & "',"
SQL += "'" & UserIp & "',"
SQL += "'" & UserAgent & "',"
SQL += "'" & UserReferer & "',"
SQL += "getdate(),"
SQL += "'" & result & "'"
SQL += ")"
how can I write the following code in StringBuilder.AppendFormat
what is the advantage to use Stringbuilder.AppendFormat
SQL = "INSERT INTO tblLog"
SQL += "(ACCOUNT,IP_ADDRESS,USER_
SQL += " values"
SQL += "(N'" & LCase(Trim(UserAccount)) & "',"
SQL += "'" & UserIp & "',"
SQL += "'" & UserAgent & "',"
SQL += "'" & UserReferer & "',"
SQL += "getdate(),"
SQL += "'" & result & "'"
SQL += ")"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
saloj,
StringBuilder is faster for excessive string manipulation.
it uses parameters like tokens, which is easy to understand.
http://msdn.microsoft.com/en-us/library/hdekwk0b.aspx
String concatenation is very confusing when ur using for subqueries or longer strings with paramteres.
thanks,
StringBuilder is faster for excessive string manipulation.
it uses parameters like tokens, which is easy to understand.
http://msdn.microsoft.com/en-us/library/hdekwk0b.aspx
String concatenation is very confusing when ur using for subqueries or longer strings with paramteres.
thanks,
This is a lil more concise:
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO tblLog (ACCOUNT,IP_ADDRESS,USER_A GENT,REFER ER,LOGIN_D ATE,LOGIN_ RESULT_TYP E)");
sb.AppendFormat(" values (N'{0}', '{1}', '{2}', '{3}', getdate(), '{4}')"
UserAccount.Trim().ToLower (), UserIp, UserAgent, UserReferer, result);
Sorry, could not convert to vb.net.
Arun
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO tblLog (ACCOUNT,IP_ADDRESS,USER_A
sb.AppendFormat(" values (N'{0}', '{1}', '{2}', '{3}', getdate(), '{4}')"
UserAccount.Trim().ToLower
Sorry, could not convert to vb.net.
Arun
Here's is a post from this morning about this issue:
https://www.experts-exchange.com/questions/26299354/When-to-use-string-format.html
Arun
https://www.experts-exchange.com/questions/26299354/When-to-use-string-format.html
Arun
I finally got the VB version:
Dim sb As New StringBuilder()
sb.Append("INSERT INTO tblLog (ACCOUNT,IP_ADDRESS,USER_A GENT,REFER ER,LOGIN_D ATE,LOGIN_ RESULT_TYP E)")
sb.AppendFormat(" values (N'{0}', '{1}', '{2}', '{3}', getdate(), '{4}')", UserAccount.Trim().ToLower (), UserIp, UserAgent, UserReferer, result)
Arun
Dim sb As New StringBuilder()
sb.Append("INSERT INTO tblLog (ACCOUNT,IP_ADDRESS,USER_A
sb.AppendFormat(" values (N'{0}', '{1}', '{2}', '{3}', getdate(), '{4}')", UserAccount.Trim().ToLower
Arun
ASKER
Hi Guys, Thanks
i am trying to do following but getting exception error. pls help
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS, USER_AGENT ,REFERER,L OGIN_DATE, LOGIN_RESU LT_TYPE)")
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0} ',", LCase(EF.Text_To_Msdb(Trim (UserAccou nt))))
SQLBD.AppendFormat("'{1}', ", UserIp)
SQLBD.AppendFormat("'{2}', ", UserAgent)
SQLBD.AppendFormat("'{3}', ", UserReferer)
SQLBD.AppendFormat("getdat e(),")
SQLBD.AppendFormat("'{4}') ", result(0))
Thanks
i am trying to do following but getting exception error. pls help
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS,
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0}
SQLBD.AppendFormat("'{1}',
SQLBD.AppendFormat("'{2}',
SQLBD.AppendFormat("'{3}',
SQLBD.AppendFormat("getdat
SQLBD.AppendFormat("'{4}')
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
change it like this paramter index number starts from 0, if you combine all the lines to one appendformat then use {0},{1}...
try this
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS, USER_AGENT ,REFERER,L OGIN_DATE, LOGIN_RESU LT_TYPE)")
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0} ',", LCase(EF.Text_To_Msdb(Trim (UserAccou nt))))
SQLBD.AppendFormat("'{0}', ", UserIp)
SQLBD.AppendFormat("'{0}', ", UserAgent)
SQLBD.AppendFormat("'{0}', ", UserReferer)
SQLBD.AppendFormat("getdat e(),")
SQLBD.AppendFormat("'{0}') ", result(0))
or this
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS, USER_AGENT ,REFERER,L OGIN_DATE, LOGIN_RESU LT_TYPE)")
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0} ','{1}','{ 2}','{3}', getDate(), '{4}')", LCase(EF.Text_To_Msdb(Trim (UserAccou nt))), UserIp, UserAgent, UserReferer, result(0))
try this
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS,
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0}
SQLBD.AppendFormat("'{0}',
SQLBD.AppendFormat("'{0}',
SQLBD.AppendFormat("'{0}',
SQLBD.AppendFormat("getdat
SQLBD.AppendFormat("'{0}')
or this
SQLBD.Append("INSERT INTO tblLog(ACCOUNT,IP_ADDRESS,
SQLBD.AppendFormat(" values")
SQLBD.AppendFormat("(N'{0}
what exception did you get ??
See the value of 'SQLBD.ToString()' in watch window it should look like your normal query.
See the value of 'SQLBD.ToString()' in watch window it should look like your normal query.
dim SQL As New StringBuilder()
SQL.append "INSERT INTO tblLog"
SQL.append "(ACCOUNT,IP_ADDRESS,USER_
SQL.append " values"
SQL.append "(N'" & LCase(Trim(UserAccount)) & "',"
SQL.append "'" & UserIp & "',"
SQL.append "'" & UserAgent & "',"
SQL.append "'" & UserReferer & "',"
SQL.append "getdate(),"
SQL.append "'" & result & "'"
SQL.append ")"