如何在包含变量的逐字字符串中转义双引号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在包含变量的逐字字符串中转义双引号相关的知识,希望对你有一定的参考价值。

我有逐字符串有一些变量contacanete with,我的问题我总是从编译器得到这个错误:

; expected

那么我怎么能正确地逃脱呢?

int teacherId = sqlStatus = (int)sqlCmd.LastInsertedId;
sqlCmd.CommandText = @"INSERT INTO report (report_type, report_date, semester, report_details, teacher_id) 
                            VALUES (0, ""2018-01-01"", 1, '{
                              ""teacherId"": "" " + teacherId.ToString() + " "",   <=== error here (; expected)
                              ""mahderDate"": """",
                              ""teacherShool"": """",
                              ""baladia"": """",
                              ""wilaya"": """",
                              ""moufatecheReport"": """"
                            }'," + teacherId + ");";
答案

使用@poke建议我设法让它在行中放置双引号和@:

    int teacherId = sqlStatus = (int)sqlCmd.LastInsertedId;
    sqlCmd.CommandText = @"INSERT INTO report (report_type, report_date, semester, report_details, teacher_id) 
                                VALUES (0, ""2018-01-01"", 1, '{
                                  ""teacherId"": "" " + teacherId.ToString()  + "," + 
                               @" ""mahderDate"": """",
                                  ""teacherShool"": """",
                                  ""baladia"": """",
                                  ""wilaya"": """",
                                  ""moufatecheReport"": """"
                                }'," + teacherId + ");";

以上是关于如何在包含变量的逐字字符串中转义双引号的主要内容,如果未能解决你的问题,请参考以下文章

我可以在逐字字符串文字中转义双引号吗?

如何在标题属性中转义双引号

如何在 T-SQL 中的 XML 字符串中的属性中转义双引号?

如何在nmake中转义双引号

如何在 PHP 的 mysqli_query() 中转义双引号字符?

如何在 django 标签中转义双引号?