在C#中禁用SQL的foregin键约束时出现语法错误 [英] Getting syntax error while disable foregin key constraints of SQL in C#

查看:180
本文介绍了在C#中禁用SQL的foregin键约束时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建和更新两个表

一个表有其他表的foregin键

更新表我收到错误所以我想关闭
oregin key。但它显示语法错误。 Blow是Code.I我也无法运行



 SqlCommand scmd = new SqlCommand(EXEC sp_MSforeachtableALTER TABLE?NOCHECK CONSTRAINT all ,con); 





我的尝试:



我检查了解决方案但是失败了

解决方案

嗯...双引号结束了一个C#字符串......



如果你想将双引号传递给SQL,你需要转义它:

 SqlCommand scmd = new SqlCommand(EXEC sp_MSforeachtable \ ALTER TABLE?NOCHECK CONSTRAINT all\,con); 

 SqlCommand scmd = new SqlCommand(@EXEC sp_MSforeachtableALTER TABLE?NOCHECK CONSTRAINT all ,con); 

但是你可能会发现这对SQL更好:

 SqlCommand scmd = new SqlCommand(EXEC sp_MSforeachtable'ALTER TABLE?NOCHECK CONSTRAINT all',con); 


I am creating and updating two tables
one table is having foregin key with other table
while updating table I am getting error so I want to turn off
oregin key while update.so I wrote one command As Mentioned in net. But it is showing syntax error. Blow is the Code.I am unable to run also

SqlCommand scmd = new SqlCommand("EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"", con);



What I have tried:

I checked for solution but failed

解决方案

Well... double quote ends a C# string ...

So you need to escape it if you want to pass a double quote to SQL:

SqlCommand scmd = new SqlCommand("EXEC sp_MSforeachtable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"", con);

Or

SqlCommand scmd = new SqlCommand(@"EXEC sp_MSforeachtable ""ALTER TABLE ? NOCHECK CONSTRAINT all""", con);

But you might find that this works better with SQL:

SqlCommand scmd = new SqlCommand("EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'", con);


这篇关于在C#中禁用SQL的foregin键约束时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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