用字符串变量的值合并双引号 [英] Concatinating Double Quotes with value of a string variable

查看:111
本文介绍了用字符串变量的值合并双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string addquery;
foreach (DataRow dr in dt.Rows)
{
  fieldname = dr["FIELDNAME"].ToString().Trim();
  addquery = addquery + ","\"" + fieldname+"\";
  messageBox.Show(addquery);
}



输出:
"Book_id","Book_name","Book_author"


消息框为我提供了所需的输出..但是当我将此变量传递给插入查询时,它会通过异常和sqlcommandtext更改如下:

"\ Book_id \","\ Book_name \","\ Book_author \"
................................................... ..................................

我需要的是使用上面所需的Output动态创建插入查询..
但是由于我有异常,sql将反斜杠放入字符串中..

问候,
Hassan Tariq



OUTPUT:
"Book_id","Book_name","Book_author"


the messagebox is giving me the required output..but when i pass this variable into the insert query it through an exception and sqlcommandtext changes like that:

"\Book_id\","\Book_name\","\Book_author\"
....................................................................................

All i need is to create insert query dynamically with the required above Output..
but sql puts backward slash into string due to which i got exception..

Regards,
Hassan Tariq

推荐答案

尝试使用单引号而不是双引号.这是TSQL和所有其他方言AFAIK的常规文本修饰符.
Try using single quotes instead of double quotes. This is the normal text decorator for TSQL and all other dialects AFAIK


如果要在TSQL中使用单引号,则应使用单引号.

如果要使双引号逻辑起作用,请执行以下操作.

1)在带双引号的字符串之前使用@.
2)使用StringBuilder,与常规的字符串操作相比,它将提供更好的性能.
3)确保要在"foreach"循环内或外部显示messageBox.


You shoud be using Single Quotes, incase if you are doing this for TSQL.

If you want to make your Double Quotes logic working then, do as below.

1) Use @ before your string which has Double Quote in it.
2) Use StringBuilder which will give you better performance as compare to regular string operations.
3) Make sure, you want to show messageBox inside "foreach" loop or outside.


System.Text.StringBuilder addquery = new System.Text.StringBuilder();
string fieldname = string.Empty;

        foreach (DataRow dr in dt.Rows)
        {
          fieldname = dr["FIELDNAME"].ToString().Trim();

          addquery.Append(@"""");
          addquery.Append(fieldname);
          addquery.Append(@""",");
          messageBox.Show(addquery);
        }


它可能对您有帮助,

步骤1)要显示在MessageBox 上,您可以尝试以下操作,而不是",这里我使用了''

It might help you,

Step 1) To display on the MessageBox you might try below, instead " here I used ''

System.Text.StringBuilder addquery = new System.Text.StringBuilder();
string fieldname = string.Empty;
foreach (DataRow dr in dt.Rows)
{
    fieldname = dr["FIELDNAME"].ToString().Trim();
    addquery.Append(@"'");
    addquery.Append(fieldname);
    addquery.Append(@"',");
    MessageBox.Show(addquery.ToString().Replace("'", "\""));
}



步骤2)对于Insert语句,您可以在下面尝试,例如,



Step 2) For Insert statement you could try below for example,

SqlCommand command = new SqlCommand();
command.CommandText = addquery.ToString();



:)



:)


这篇关于用字符串变量的值合并双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆