MS-SQL Server 中单引号的两种处理方法
MS-SQL Server 中单引号的两种处理方法
和数据库打交道要频繁地用到 SQL 语句,除非你是全部用控件绑定的方式,但采用控件绑定的方式存在着灵活性差、效率低、功能弱等等缺点。因此,大多数的程序员极少或较少用这种绑定的方式。而采用非绑定方式时许多程序员大都忽略了对单引号的特殊处理,一旦SQL语句的查询条件的变量有单引号出现,数据库引擎就会报错指出SQL语法不对,本人发现有两种方法可以解决和处理这种单引号的问题(以VB为例子)。---- 方法一:利用转义字符处理SQL语句。下面的函数可以在执行SQL语句前调用,执行处理后的结果即可产生正确的结果。
代码如下:Function ProcessStr(str As String)
Dim pos As Integer
Dim stedest As String
pos = InStr(str, “'“)
While pos 〉0
str = Mid(str, 1, pos) & “'“ & Mid(str, pos + 1)
pos = InStr(pos + 2, str, “'“)
Wend
ProcessStr = str
End Function
---- 其中str参数是你的SQL字符串。函数一旦发现字符串中有单引号出现,就在前面补上一个单引号。
---- 方法二:利用数据对象中的参数。可以利用ADODB.COMMAND对象,把含有单引号的字符串传递给COMMAND,然后执行查询等操作即可。
---- 以上两种方法比较,方法一增加了系统处理时间,方法二简洁、高效,如果采用存储过程,然后再传递参数给存储过程,存储过程是预编译的,这样系统的效率更高。
---- 下面就举例子加以说明。
---- 新建一个项目,项目中有一个窗体(Form1),两个命令按钮,一个MSFlexGrid,名称分别为:Command1,Command2,MSFlexGrid1,一个COMBOX(COMBO1),它的内容预先设定为“Paolo''f“、“Paolo'f“。Command1演示方法一,Command2演示方法二,MSFlexGrid1存储方法二查询(SELECT)结果。对于其他的SQL操作(INSERT、DELTER、UPDATAE)方法极为类似,笔者就不再赘述。例子中用到SQL SERVER中的PUBS数据库中的EMPLOYEE表,同时可以用SQL语法把其中两条记录中的FNAME改为“Paolo''f“、“Paolo'f“。 SQL语法如下:
update employee set fname=“ Paolo''''f“
where emp_id='PMA42628M'
update employee set fname=“ Paolo''f“
where emp_id='PMA42628M'
---- 程序如下:
---- 首先把前面的函数加入。
---- 在窗体的通用中声明如下变量:
Dim cnn1 As ADODB.Connection '连接
Dim mycommand As ADODB.Command '命令
Dim rstByQuery As ADODB.Recordset '结果集
Dim strCnn As String '连接字符串
Private Sub Form_Load()
Set cnn1 = New ADODB.Connection '生成一个连接
strCnn = “driver={SQL Server};“ & _
“server=ZYX_pc;uid=sa;pwd=PCDC;database=pubs“ '
没有系统数据源使用连接字符串
'strCnn = “DSN=mydsn;UID=sa;PWD=;“
'DATABASE=pubs;Driver={SQL Server};SERVER=gzl_pc“ '
如果系统数据源MYDSN指向PUBS数据库,也可以这样用
cnn1.Open strCnn, , , 0 '打开连接
End Sub
Private Sub Command1_Click() '演示字符处理
Dim i As Integer
Dim j As Integer
Set parm = New ADODB.Parameter
Set mycommand = New ADODB.Command
Dim str As String
str = Combo1.Text
str = ProcessStr (str)
mycommand.ActiveConnection = cnn1 '
指定该command 的当前活动连接
mycommand.CommandText = “ select * from
employee where fname = '“ & str & “'“
mycommand.CommandType = adCmdText '表明command 类型
Set rstByQuery = New ADODB.Recordset
Set rstByQuery = mycommand.Execute()
i = 0
Do While Not rstByQuery.EOF
i = i + 1 ' i 中保存记录个数
rstByQuery.MoveNext
Loop
MSFlexGrid1.Rows = i + 1 '动态设置MSFlexGrid的行和列
MSFlexGrid1.Cols = rstByQuery.Fields.count + 1
MSFlexGrid1.Row = 0
For i = 0 To rstByQuery.Fields.count - 1
MSFlexGrid1.Col = i + 1
MSFlexGrid1.Text = rstByQuery.Fields.Item(i).Name
Next '设置第一行的标题,用域名填充
i = 0
'Set rstByQuery = mycommand.Execute()
rstByQuery.Requery
Do While Not rstByQuery.EOF
i = i + 1
MSFlexGrid1.Row = i '确定行
For j = 0 To rstByQuery.Fields.count - 1
MSFlexGrid1.Col = j + 1
MSFlexGrid1.Text = rstByQuery(j) '添充所有的列
Next
rstByQuery.MoveNext
Loop '这个循环用来填充MSFlexGrid的内容
End Sub
Private Sub Command2_Click()'参数方法
Dim i As Integer
Dim j As Integer
Set parm = New ADODB.Parameter
Set mycommand = New ADODB.Command
' parm_jobid.Name = “name1“ this line can be ommited
parm.Type = adChar '参数类型
parm.Size = 10 '参数长度
parm.Direction = adParamInput '参数方向,输入或输出
parm.Value = Combo1.Text '参数的值
mycommand.Parameters.Append parm '加入参数
mycommand.ActiveConnection = cnn1 '
指定该command 的当前活动连接
mycommand.CommandText = “ select *
from employee where fname =? “
mycommand.CommandType = adCmdText '表明command 类型
Set rstByQuery = New ADODB.Recordset
Set rstByQuery = mycommand.Execute()
i = 0
Do While Not rstByQuery.EOF
i = i + 1 ' i 中保存记录个数
rstByQuery.MoveNext
Loop
MSFlexGrid1.Rows = i + 1 '动态设置MSFlexGrid的行和列
MSFlexGrid1.Cols = rstByQuery.Fields.count + 1
MSFlexGrid1.Row = 0
For i = 0 To rstByQuery.Fields.count - 1
MSFlexGrid1.Col = i + 1
MSFlexGrid1.Text = rstByQuery.Fields.Item(i).Name
Next '设置第一行的标题,用域名填充
i = 0
rstByQuery.Requery
Do While Not rstByQuery.EOF
i = i + 1
MSFlexGrid1.Row = i '确定行
For j = 0 To rstByQuery.Fields.count - 1
MSFlexGrid1.Col = j + 1
MSFlexGrid1.Text = rstByQuery(j) '添充所有的列
Next
rstByQuery.MoveNext
Loop '这个循环用来填充MSFlexGrid的内容
End Sub
---- 查询部分可以用存储过程以提高处理效率,减低网络流量。
---- 本程序在NT WORKSTATION 4.0 SP4、SQL SERVER 7.0 上调试通过

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.
