C#中的Ad Hoc查询 [英] Ad Hoc Query in C#

查看:68
本文介绍了C#中的Ad Hoc查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的......我们走了......


我正在尝试从c#手动运行SQL服务器备份作业。这是我的代码开始这个过程的

片段:


string strSQL =" USE msdb" +

" \ n if exists(select * from sysjobs_view where name =''TempBackupJob'')" +

" \ n EXEC sp_delete_job @job_name =''TempBackupJob''" +

" \ n GO";

newDB.Command(strSQL);


此命令例外情况带有& ;'GO''附近的语法不正确。

现在,当我将EXACT相同的查询放入查询分析器时,这样:


USE msdb

如果存在(select * from sysjobs_view where name =''TempBackupJob'')

EXEC sp_delete_job @job_name =''TempBackupJob''

GO


命令执行没有任何错误。


换行字符有什么我想念的吗?我也尝试使用

\\ n而不是仅仅\ n来分隔线......这些也没用。


非常感谢任何帮助。

-

谢谢,


John Scott。

Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = ''TempBackupJob'') " +
"\n EXEC sp_delete_job @job_name = ''TempBackupJob'' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near ''GO''".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = ''TempBackupJob'')
EXEC sp_delete_job @job_name = ''TempBackupJob''
GO

the command executes without any errors.

Is there something I''m missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn''t work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.

推荐答案

取出回车和换行符


/ + /'s会让你打破c#中的行代码


在第一个前面使用@ (双引号)


string strSQL = @" USE msdb" +

" if exists(select * from sysjobs_view where name =''TempBackupJob'')" +

" EXEC sp_delete_job @job_name =''TempBackupJob''" +

" GO"

John Scott写道:
take out the carriage returns and line breaks

The /+/''s will let you break the lines in your c# code

Use an @ in front of the first " (double quote)

string strSQL = @"USE msdb " +
" if exists(select * from sysjobs_view where name = ''TempBackupJob'') " +
" EXEC sp_delete_job @job_name = ''TempBackupJob'' " +
" GO";
John Scott wrote:
好的......我们走了......

我正在尝试手动运行SQL服务器从c#备份工作。以下是开始此过程的代码片段:

string strSQL =" USE msdb" +
" \ n if exists(从sysjobs_view中选择*,其中name =''TempBackupJob'')" +
" \ n EXEC sp_delete_job @job_name =''TempBackupJob''" +
" \ n GO" ;;
newDB.Command(strSQL);

此命令异常,GO''和附近的语法不正确。
现在,当我将EXACT相同的查询放入查询分析器时,如下所示:

USE msdb
if exists(select * from sysjobs_view where name =''TempBackupJob'')
EXEC sp_delete_job @job_name =''TempBackupJob''


命令执行没有任何错误。

有什么我想念的东西换行符?我也尝试使用
\\\\ n而不是仅仅\ n来分隔线......这些也没有用。

任何帮助都将不胜感激。
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = ''TempBackupJob'') " +
"\n EXEC sp_delete_job @job_name = ''TempBackupJob'' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near ''GO''".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = ''TempBackupJob'')
EXEC sp_delete_job @job_name = ''TempBackupJob''
GO

the command executes without any errors.

Is there something I''m missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn''t work either.

Any help would be greatly appreciated.



为什么需要\ n和\r?这不像你必须打印出sql

字符串才能让它可读。你只需要执行它并考虑到它是SQL所需要的SQL是你的SQL语句和

命令之间的良好间距。


尝试没有\r,没有\ n,看看会发生什么..

祝你好运


Amadelle


" John Scott"写道:
Why do you need the \n and \r ? It is not like you have to print out the sql
string to make it readable. You just need to execute it and considering that
it is SQL all you need is good spacing between your SQL statements and
commands.

Try it with no \r and no \n and see what happens..

Good luck

Amadelle

"John Scott" wrote:
好的......我们走了......

我正在尝试从c#手动运行SQL服务器备份作业。以下是开始此过程的代码片段:

string strSQL =" USE msdb" +
" \ n if exists(从sysjobs_view中选择*,其中name =''TempBackupJob'')" +
" \ n EXEC sp_delete_job @job_name =''TempBackupJob''" +
" \ n GO" ;;
newDB.Command(strSQL);

此命令异常,GO''和附近的语法不正确。
现在,当我将EXACT相同的查询放入查询分析器时,如下所示:

USE msdb
if exists(select * from sysjobs_view where name =''TempBackupJob'')
EXEC sp_delete_job @job_name =''TempBackupJob''


命令执行没有任何错误。

有什么我想念的东西换行符?我也尝试使用
\\\\ n而不是仅仅\ n来分隔线......这些也没有用。

任何帮助都将不胜感激。

-
谢谢,

John Scott。
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = ''TempBackupJob'') " +
"\n EXEC sp_delete_job @job_name = ''TempBackupJob'' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near ''GO''".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = ''TempBackupJob'')
EXEC sp_delete_job @job_name = ''TempBackupJob''
GO

the command executes without any errors.

Is there something I''m missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn''t work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.



当我拿出命令异常也是\\\\ n。(不正确的语法

''GO'')

我还在Query Anaylzer中测试了一些东西..如果SQL声明全部在一个

行......它失败了......如果其他一切除了GO在同一行......

查询成功运行。所以...我认为GO在评估时必须在其自己的行上使用GO。这就是为什么我想知道线路

休息。


还有别的吗?


- -

谢谢,


John Scott。

" Amadelle"写道:
When I take out the \r\n the command exceptions too.( Incorrect syntax near
''GO'')
I also tested something in Query Anaylzer..if the SQL statment is all on one
line...it fails...if everything else EXCEPT GO is on the same line...the
query runs successfully. So...I am under the assumption that GO has to be on
its own line when it is evaluated. That''s why I was wondering about line
breaks.

Got anything else ??

--
Thanks,

John Scott.
"Amadelle" wrote:
为什么需要\\\
和\ r?这不像你必须打印出sql
字符串以使其可读。你只需要执行它并考虑到它是SQL所需要的只是你的SQL语句和
命令之间的良好间隔。

尝试没有\r和没有嗯,看看会发生什么..

祝你好运

Amadelle

John Scott写道:
Why do you need the \n and \r ? It is not like you have to print out the sql
string to make it readable. You just need to execute it and considering that
it is SQL all you need is good spacing between your SQL statements and
commands.

Try it with no \r and no \n and see what happens..

Good luck

Amadelle

"John Scott" wrote:
好的......我们走了......

我正在尝试从c#手动运行SQL服务器备份作业。以下是开始此过程的代码片段:

string strSQL =" USE msdb" +
" \ n if exists(从sysjobs_view中选择*,其中name =''TempBackupJob'')" +
" \ n EXEC sp_delete_job @job_name =''TempBackupJob''" +
" \ n GO" ;;
newDB.Command(strSQL);

此命令异常,GO''和附近的语法不正确。
现在,当我将EXACT相同的查询放入查询分析器时,如下所示:

USE msdb
if exists(select * from sysjobs_view where name =''TempBackupJob'')
EXEC sp_delete_job @job_name =''TempBackupJob''


命令执行没有任何错误。

有什么我想念的东西换行符?我也尝试使用
\\\\ n而不是仅仅\ n来分隔线......这些也没有用。

任何帮助都将不胜感激。

-
谢谢,

John Scott。
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = ''TempBackupJob'') " +
"\n EXEC sp_delete_job @job_name = ''TempBackupJob'' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near ''GO''".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = ''TempBackupJob'')
EXEC sp_delete_job @job_name = ''TempBackupJob''
GO

the command executes without any errors.

Is there something I''m missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn''t work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.



这篇关于C#中的Ad Hoc查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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