SQL 用撇号插入数据库 [英] SQL insert into database with apostrophe

查看:32
本文介绍了SQL 用撇号插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经典 ASP 上运行一个程序,并使用以下内容插入数据库:

I am running a program on classic ASP and inserting into a database with the following:

CreateJob.CommandText = "INSERT INTO dbo.Jobs (JobID, CompanyName, DateReceived, DateOfDocument, ClientReference, Subject, TypeOfService,DueDate,AssignedAgent, ClientName, Plaintiff, Defendant1, Defendant2, Defendant3, CourtJurisdiction, Court, Subtype, CourtNumber, Amount, ServiceMethod, JobNotes, JobStatus, CreatedBy, CreatedDate)  VALUES (" & Request.Form("jobid") & ", '""" & Request.Form("compname") & """', '" & Request.Form("datereceived") & "','" & Request.Form("dateofdoc") & "', '" & Request.Form("clientref") & "', '" & Request.Form("subjects") & "', '" & Request.Form("TypeOfService") & "', '" & Request.Form("duedate") & "', '" & Request.Form("AssignedAgent") & "', '" & Request.Form("ClientName") & "', '" & Request.Form("Plaintiff") & "', '" & Request.Form("Defendant1") & "', '" & Request.Form("Defendant2") & "', '" & Request.Form("Defendant3") & "', '" & Request.Form("CourtJurisdiction") & "', '""" &  Request.Form("Court") & """', '" & Request.Form("SubType") & "', '" & Request.Form("CourtNumber") & "', '" & Request.Form("Amount") & "','" & Request.Form("ServiceMethod") & "','" & Request.Form("JobNotes") & "', 'OPEN', '" & Session("LoggedName") & "', CURRENT_TIMESTAMP ) "

但是,如果其中一个值有撇号,程序就会崩溃,我不知道如何转义它.

However, if one of the value has an apostrophe, the program crashes and I am not sure how to escape it.

谢谢

推荐答案

Replace 不是解决方案,您已经在使用 ADODB.Command 对象,为什么不使用 参数化查询.

Replace isn't the way to go here, you are already using a ADODB.Command object so why not use a parameterised query.

试试这个;

由于您尚未提供有关字段类型的信息,我只能推测,因此我添加了 [datatype][size] 占位符供您替换使用 ADO 数据类型常量.关于 T-SQL 中的数据类型如何映射到 数据类型映射

As you haven't provided information on your field types I can only speculate so instead I've added [datatype] and [size] placeholders for you to replace with ADO data type constants. A good resource for how data types in T-SQL map to ado is this article - Data Type Mapping

sql = ""
sql = sql & "INSERT INTO dbo.Jobs (" & vbCrLf
sql = sql & "JobID, CompanyName, DateReceived, DateOfDocument, ClientReference" & vbCrLf
sql = sql & ", Subject, TypeOfService,DueDate,AssignedAgent, ClientName, Plaintiff" & vbCrLf
sql = sql & ", Defendant1, Defendant2, Defendant3, CourtJurisdiction, Court" & vbCrLf
sql = sql & ", Subtype, CourtNumber, Amount, ServiceMethod, JobNotes, JobStatus" & vbCrLf
sql = sql & ", CreatedBy, CreatedDate" & vbCrLf
sql = sql & ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"

With CreateJob
    .ActiveConnection = "yourconnectionstring"
    .CommandType = adCmdText
    .CommandText = sql
    'Add your parameters (all 24 of them in order)
    'Assumed JobID is int which equates to adInteger ADO data type constant.
    .Parameters.Append(.CreateParameter("@JobID", adInteger, adParamInput, 4))
    .Parameters.Append(.CreateParameter("@CompanyName", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@DateReceived", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@DateOfDocument", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@ClientReference", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Subject", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@TypeOfService", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@DueDate", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@AssignedAgent", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@ClientName", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Plaintiff", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Defendant1", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Defendant2", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Defendant3", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@CourtJurisdiction", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Court", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Subtype", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@CourtNumber", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@Amount", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@ServiceMethod", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@JobNotes", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@JobStatus", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@CreatedBy", [datatype], adParamInput, [size]))
    .Parameters.Append(.CreateParameter("@CreatedDate", [datatype], adParamInput, [size]))

    'Specify your parameter values may need some conversion based on what you are passing.
    .Parameters("@JobId").Value = Request.QueryString("jobid")
    'Add the other 23 parameters as the above line.
    '...

    'Doing an INSERT no need to return recordset
    Call .Execute(adExecuteNoRecords)
End With
Set CreateJob = Nothing

这篇关于SQL 用撇号插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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