转义ormlite中insert(来自动态创建的表)中的特殊字符

时间:2017-08-07 09:49:13

标签: c# sql-server servicestack special-characters ormlite

我正在使用C#,我使用ServiceStack.OrmLite创建了一个表,对应于在运行时创建的类类型。为了清楚起见,我做了以下事情:

  • 在运行时创建类型(employeeType)后,我可以执行以下操作:

db.CreateTableIfNotExists(employeeType);

这将创建对应于(动态创建的类型“Employee”)的表Employee。

  • 实际上,我的问题是Employee表列名包含一些特殊字符(例如:%,/ ...等)。正如我刚刚描述的那样,创建表运行良好,但是当我尝试在此表中插入一行时,由于SQL语句中出现语法错误(由于列名中的特殊字符),因此出现异常。我的理由如下(检查this):

System.Data.SqlClient.SqlErrorCollection: Incorrect syntax near '('.\r\nMust declare the scalar variable \"@TokenRequestsPerSec_Request\"."

有关信息,我在表中的列的名称是“TokenRequestsPerSec_Request \ Seconds”,因此问题在于“\”

我使用以下内容在Employee表中插入一列:

var typedApi = db.CreateTypedApi(employeeType);

typedApi.Insert(employeeInstance);

请检查here以查看OrmLite对SQL Server执行的插入语句:

exec sp_executesql N'INSERT INTO "ScanADFS" ("MonitoredObjectId","ScanDate","ScanDateInMs","EntityStatus","SystemServiceStatus","CertificateServiceStatus","FederationServiceStatus", "CpuUsage_Percentage","RamAvailable_MB","DiskTime_Percentage","AverageDiskQueueLength","SystemPingTime_ms","CertificateSigningExpirationDate","CertificateTokenDecryptingExpirationDate", "CertificateServiceCommunicationsExpirationDate","TokenRequests","TokenRequestsPerSec_Request(s)/Second","ArtifactResolutionRequests_ms","ArtifactResolutionRequestsPerSec_Request(s)/Second", "ExternalAuthenticationFailures","ExtranetAccountLockouts","FederationMetaDataRequests","FederationMetaDataRequestsPerSec_Request(s)/Second","FederatedAuthenticationFailures","PasswordChangeFailedRequests", "PasswordChangeSuccessfulRequests","SystemPingTest","SystemPowershellPerformanceCountersTest","SystemWindowsServicesTest","SystemCpuTest","SystemRamTest","SystemDiskTimeTest","SystemAverageDiskQueueTest", "SystemDiskSpaceTest","CertificateTest","FederationTest","SystemPingThreshold_ms","CpuThreshold_Percentage","RamThreshold_MB","DiskTimeThreshold_Percentage","AverageDiskQueueLengthThreshold", "CertificateValidityThreshold_Days") VALUES (@MonitoredObjectId,@ScanDate,@ScanDateInMs,@EntityStatus,@SystemServiceStatus,@CertificateServiceStatus,@FederationServiceStatus,@CpuUsage_Percentage,@RamAvailable_MB,@DiskTime_Percentage, @AverageDiskQueueLength,@SystemPingTime_ms,@CertificateSigningExpirationDate,@CertificateTokenDecryptingExpirationDate,@CertificateServiceCommunicationsExpirationDate,@TokenRequests, @TokenRequestsPerSec_Request(s)/Second,@ArtifactResolutionRequests_ms,@ArtifactResolutionRequestsPerSec_Request(s)/Second,@ExternalAuthenticationFailures,@ExtranetAccountLockouts, @FederationMetaDataRequests,@FederationMetaDataRequestsPerSec_Request(s)/Second,@FederatedAuthenticationFailures,@PasswordChangeFailedRequests,@PasswordChangeSuccessfulRequests, @SystemPingTest,@SystemPowershellPerformanceCountersTest,@SystemWindowsServicesTest,@SystemCpuTest,@SystemRamTest,@SystemDiskTimeTest,@SystemAverageDiskQueueTest,@SystemDiskSpaceTest, @CertificateTest,@FederationTest,@SystemPingThreshold_ms,@CpuThreshold_Percentage,@RamThreshold_MB,@DiskTimeThreshold_Percentage,@AverageDiskQueueLengthThreshold,@CertificateValidityThreshold_Days)' ,N'@MonitoredObjectId int,@ScanDate datetime,@ScanDateInMs bigint,@EntityStatus varchar(7),@SystemServiceStatus varchar(7),@CertificateServiceStatus varchar(2),@FederationServiceStatus varchar(2),@CpuUsage_Percentage float,@RamAvailable_MB float,@DiskTime_Percentage float,@AverageDiskQueueLength float,@SystemPingTime_ms float,@CertificateSigningExpirationDate varchar(21),@CertificateTokenDecryptingExpirationDate varchar(21),@CertificateServiceCommunicationsExpirationDate varchar(19),@TokenRequests float,@TokenRequestsPerSec_Request(s)/Second float,@ArtifactResolutionRequests_ms float,@ArtifactResolutionRequestsPerSec_Request(s)/Second float,@ExternalAuthenticationFailures float,@ExtranetAccountLockouts float,@FederationMetaDataRequests float,@FederationMetaDataRequestsPerSec_Request(s)/Second float,@FederatedAuthenticationFailures float,@PasswordChangeFailedRequests float,@PasswordChangeSuccessfulRequests float,@SystemPingTest varchar(2),@SystemPowershellPerformanceCountersTest varchar(2),@SystemWindowsServicesTest varchar(2),@SystemCpuTest varchar(2),@SystemRamTest varchar(2),@SystemDiskTimeTest varchar(2),@SystemAverageDiskQueueTest varchar(2),@SystemDiskSpaceTest float,@CertificateTest varchar(2),@FederationTest varchar(2),@SystemPingThreshold_ms float,@CpuThreshold_Percentage float,@RamThreshold_MB float,@DiskTimeThreshold_Percentage float,@AverageDiskQueueLengthThreshold float,@CertificateValidityThreshold_Days float', @MonitoredObjectId=2,@ScanDate='2017-08-07 15:27:48',@ScanDateInMs=1502112468000,@EntityStatus='Warning',@SystemServiceStatus='Warning',@CertificateServiceStatus='Up',@FederationServiceStatus='Up',@CpuUsage_Percentage=36,719999999999999,@RamAvailable_MB=592,@DiskTime_Percentage=0,@AverageDiskQueueLength=0,@SystemPingTime_ms=1,@CertificateSigningExpirationDate='5/21/2018 12:49:00 AM',@CertificateTokenDecryptingExpirationDate='5/21/2018 12:48:55 AM',@CertificateServiceCommunicationsExpirationDate='6/9/2018 5:31:25 PM',@TokenRequests=0,@TokenRequestsPerSec_Request(s)/Second=0,@ArtifactResolutionRequests_ms=0,@ArtifactResolutionRequestsPerSec_Request(s)/Second=0,@ExternalAuthenticationFailures=0,@ExtranetAccountLockouts=0,@FederationMetaDataRequests=63876,@FederationMetaDataRequestsPerSec_Request(s)/Second=1,@FederatedAuthenticationFailures=0,@PasswordChangeFailedRequests=0,@PasswordChangeSuccessfulRequests=0,@SystemPingTest='Up',@SystemPowershellPerformanceCountersTest='Up',@SystemWindowsServicesTest='Up',@SystemCpuTest='Up',@SystemRamTest='Up',@SystemDiskTimeTest='Up',@SystemAverageDiskQueueTest='Up',@SystemDiskSpaceTest=2,@CertificateTest='Up',@FederationTest='Up',@SystemPingThreshold_ms=275,@CpuThreshold_Percentage=80,@RamThreshold_MB=500,@DiskTimeThreshold_Percentage=90,@AverageDiskQueueLengthThreshold=2,@CertificateValidityThreshold_Days=30

是否有解决方案来转义OrmLite插入语句中的特殊字符?

感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

您可以尝试这样

String query =“从用户中选择*,其中id =?ORDER BY \”usernum#\“”;

通过“username#”desc;

从测试订单中选择*