开发者

Handle NUL (\0) in C#

I'm retrieving data from a .xls file using oledb and assigning it to a string开发者_如何学Go variable Values using below line:

values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "',";

It woks fine for best case, but if any cell contains the value \0, thereafter values variable will not update with further values. How can I get rid from it?


Well you can get rid of null characters from a string really easily:

text = text.Replace("\0", "");

... but is there any way you can use a prepared statement instead of putting the values directly into the SQL? Or does OLEDB not support that?


You can use conditional operator(condition??true:false) to check whether the coming value is \0 or not i just Modify this one as

values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()!="\0"?row[i].ToString():"") + "',";

or you can use this one.

values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()??"") + "',";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜