简体   繁体   English

如何为.NET DbType获取正确的SQL带引号的字符串

[英]How do I get the correct sql quoted string for a .NET DbType

I want to run an ALTER TABLE that adds a default value constraint to a column. 我想运行一个将默认值约束添加到列的ALTER TABLE

I generate this statement dynamically from a .NET program. 我从.NET程序动态生成此语句。

How do I best format and quote the value when building my sql - now that ALTER TABLE statements does not support parameters (Gives the error 'Variables are not allowed in the ALTER TABLE statement'). 既然构建ALTER TABLE语句不支持参数,那么如何最好地格式化和引用该值(既然ALTER TABLE语句不支持参数(给出错误“ ALTER TABLE语句中不允许变量”))。

Is there a utility for that in .NET? .NET中是否有实用程序? Or another solution? 还是其他解决方案?

string.Format("alter table YourTable add constraint DF_YourTable_Col1 default '{0}'",
    inputValue.Replace("'", "''"));

You can do this in TSQL; 您可以在TSQL中执行此操作; for example, say you parameterize the command, passing in @DefaultValue , a varchar which may or may not be a valid TSQL literal. 例如,假设您对命令进行参数化,传入@DefaultValue ,即一个varchar ,它可能是也可能不是有效的TSQL文字。 Because we are writing DDL, we will need to concatenate and exec , however we clearly don't want to blindly concatenate, as the value could be illegal. 因为我们正在编写DDL,所以我们需要串联和exec ,但是我们显然不想盲目串联,因为该值可能是非法的。 Fortunately, quotename does everything we need. 幸运的是, quotename我们的所有需求。 By default, quotename outputs [qualified object names] , but you can tell it to operate in literal escaping mode, for both single-quote and double-quote literals. 默认情况下, quotename输出[qualified object names] ,但是您可以告诉它在单引号和双引号文字中都以文字转义模式运行。

So our query that accepts @DefaultValue can build an SQL string: 因此,接受@DefaultValue的查询可以构建一个SQL字符串:

declare @sql nvarchar(4000) = 'alter table ...';
-- ... blah

-- append the default value; note the result includes the outer quotes
@sql = @sql + quotename(@DefaultValue, '''');
-- ... blah

exec (@sql);

Full example: 完整示例:

--drop table FunkyDefaultExample
create table FunkyDefaultExample (id int not null)

declare @tableName varchar(20) = 'FunkyDefaultExample',
        @colName varchar(20) = 'col name',
        @defaultValue varchar(80) = 'test '' with quote';

-- the TSQL we want to generate to exec
/*
alter table [FunkyDefaultExample] add [col name] varchar(50) null
      constraint [col name default] default 'test '' with quote';
*/
declare @sql nvarchar(4000) = 'alter table ' + quotename(@tablename)
    + ' add ' + quotename(@colName) + 'varchar(50) null constraint '
    + quotename(@colName + ' default') + ' default '
    + quotename(@defaultValue, '''');

exec (@sql);
-- tada!

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

相关问题 如何使用LINQ从带分隔符的字符串中获取带引号的字段作为未带引号的值的列表? - How do I get quoted fields from a delimited string as a list of unquoted values using LINQ? 如何拆分搜索字符串以允许带引号的文本? - How do I split up a search string to allow for quoted text? 如何在C#中的字符串中保留多个引号 - How do I maintain multiple quotes quoted in a string in c# 不知道为什么在C#中,我从SQL命令参数对象中的DbType获得了完全不同的TypeCode? - Not sure why in c# I get a totally different TypeCode from a DbType within a SQL command parameter object? 如何将消息作为字符串变量获取,Discord.Net - How do I get message as a string varible, Discord.Net 如何为ASP.NET TextBox获取正确的ClientID? - How do I get the correct ClientID for my ASP.NET TextBox? 如何在C#中将字符串转换为DbType.Date? - How to convert a string to DbType.Date in C#? 无论 .NET 中的时区如何,如何获得 FAT32 文件的正确修改日期时间? - How do I get the correct modified datetime of a FAT32 file, regardless of timezone in .NET? 如何为dbtype十进制传递空值? - How do you pass null values for dbtype decimal?? 如何从哈希集中获取正确大小写的值 <string> ? - How do I get the correct-cased value from a hashset<string>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM