简体   繁体   English

在CreateTableCommand.ExecuteNonQuery()中获取错误;

[英]Getting Error in CreateTableCommand.ExecuteNonQuery();

This is the query in which im getting error 这是我得到错误的查询

CreateTableCommand.CommandText = "BACKUP DATABASE Test" +
                             "TO  DISK = 'C:\backup\t1.bak'" + 
                                 "WITH " +
                             "NOFORMAT, " +
                             "COMPRESSION," +
                             "NOINIT,  " +
                             "NAME = N't1-Full Database Backup'," + 
                             "SKIP, " +
                                 "STATS = 10;";

and the error is "Incorrect syntax near 'DISK'." 并且错误是“DISK'附近的语法不正确。” but if i run run that query ms sql server 2008 its work fine but when i try to use that in my C# application it gave error pls help me out 但是,如果我运行该查询ms sql server 2008它的工作正常,但当我尝试在我的C#应用​​程序中使用它时它给出了错误请帮助我

Add another space after Test , your SQL is built to ... TestTO DISK ... . Test之后添加另一个空格,您的SQL构建为... TestTO DISK ...

string line2 = "BACKUP DATABASE Test ";

As Onots pointed out, you should also escape some characters properly ( \\ introduces an escape sequence, \\ itself is escaped as \\\\ ): 正如Onots指出的那样,你也应该正确地转义一些字符( \\引入转义序列, \\本身作为\\\\转义):

string line2 = "TO  DISK = \'C:\\backup\\t1.bak\'";

See also MSDN: string (C# Reference) for details. 有关详细信息,另请参见MSDN:string(C#参考)

For SQL statements, it's easier to use a Verbatim string literal for the whole query rather than concatenating fragments. 对于SQL语句,使用Verbatim字符串文字比整个查询更容易,而不是连接片段。 Eg 例如

CreateTableCommand.CommandText = @"
BACKUP DATABASE Test
TO  DISK = 'C:\backup\t1.bak'
WITH 
NOFORMAT, 
COMPRESSION,
NOINIT,
NAME = N't1-Full Database Backup',
SKIP, 
STATS = 10;
";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM