为什么在构建SQL命令时使用地址符号@?

发布于 2024-12-02 14:43:33 字数 1011 浏览 1 评论 0原文

可能的重复:
.NET 字符串前面的 @ 是什么?

这个 SQL 命令中的 @ 是做什么的?它的第一次和第二次出现会起到相同的作用吗?

static DataTable StaffsGetProjectIDHRCoordinators(SqlConnection conn, string targetDatabase)
{
    try
    {
        SqlCommand cmd = new SqlCommand(@"
            SELECT DISTINCT Staff.*
            FROM Staff
            INNER JOIN " + targetDatabase.Replace("'", "''") + @".dbo.ProjectHRCoordinator ProjectHRCoordinator
            ON Staff.StaffID = ProjectHRCoordinator.HRCoordinatorStaffID
            ORDER BY Staff.FName, Staff.LName", conn);

            return ExecuteDataTable(cmd);
    }
    finally
    {
        if (conn.State != ConnectionState.Closed) conn.Close();
    }
}

我只是熟悉声明存储过程的参数时的 @ 符号。

Possible Duplicate:
What's the @ in front of a string for .NET?

What is that @ in this SQL command doing? Would its first and second occurrence serve the same purpose?

static DataTable StaffsGetProjectIDHRCoordinators(SqlConnection conn, string targetDatabase)
{
    try
    {
        SqlCommand cmd = new SqlCommand(@"
            SELECT DISTINCT Staff.*
            FROM Staff
            INNER JOIN " + targetDatabase.Replace("'", "''") + @".dbo.ProjectHRCoordinator ProjectHRCoordinator
            ON Staff.StaffID = ProjectHRCoordinator.HRCoordinatorStaffID
            ORDER BY Staff.FName, Staff.LName", conn);

            return ExecuteDataTable(cmd);
    }
    finally
    {
        if (conn.State != ConnectionState.Closed) conn.Close();
    }
}

I'm just familiar with the @ signs when declaring a stored procedure's parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-12-09 14:43:33

这意味着该字符串是逐字字符串文字。 \ 不会以特殊方式处理。 \n 不是新行,而是斜杠和 n。对于路径非常有用。

通常,要获得包含 C:\Windows\myfile.txt 的字符串,您需要编写 "C:\\Windows\\myfile.txt" 或逐字字符串文字@“C:\Windows\myfile.txt”

2.4.4.5 字符串文字:

<块引用>

逐字字符串文字由 @ 字符后跟双引号字符、零个或多个字符以及结束双引号字符组成。一个简单的例子是@“hello”。在逐字字符串文字中,分隔符之间的字符将逐字解释,唯一的例外是引号转义序列。特别是,简单转义序列以及十六进制和 Unicode 转义序列不会按逐字字符串文字进行处理。逐字字符串文字可以跨越多行。

我将补充一点,在您引用的 SQL 块中,它用于能够在多行中编写脚本,而无需关闭字符串的每一行。所以

string str = "A " + 
    "B " + 
    "C";

你可以写

string str = @"A
    B
    C";

注意这两个字符串是非常不同的!第二个包含行尾和对齐空格,但在 SQL 中这不是问题(SQL 中许多地方都会忽略空格)。

您有可能在一行中编写了 SQL 命令。那么 @ 就完全没用了,但是许多程序员采用“安全总比后悔好”的方式,并且相当随意地使用它,特别是当他们从其他可能包含 \ escapes:-)

It means the string is a verbatim string literal. The \ won't be treated in a special way. \n isn't new line, it's slash and n. Very useful for paths.

Normally to have a string containing C:\Windows\myfile.txt you would need to write "C:\\Windows\\myfile.txt" OR with verbatim string literals @"C:\Windows\myfile.txt".

This is covered in 2.4.4.5 String literals of the C# specification:

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

I'll add that in the block of SQL you quoted it is used to be able to write the script in multiple lines without closing every line the string. So insted of

string str = "A " + 
    "B " + 
    "C";

you can write

string str = @"A
    B
    C";

Be aware that the two strings are very different! The second one contains the end-of-lines and the alignment spaces, but in SQL this isn't a problem (empty space is ignored in many places in SQL).

There is a chance that you wrote you SQL command in a single line. Then the @ was totally useless, but many programmers adopt a "better safe than sorry" and sprinkle it quite liberally, especially when they are pasting text from somewhere else that could contain \ escapes :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文