C#Windows MDB操作员窗体保存问题记录 [英] C# windows form MDB Operator Problem saving Record

查看:71
本文介绍了C#Windows MDB操作员窗体保存问题记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在gizli tarihi 中保存一个记录BOOK_NAME值" Mit" ",该值在查询表达式"Mit"中在不带()的运算符中给出错误" In" ......(''.在保存记录时.由于运算符" ''"

但是我可以直接从mdb文件保存此记录.为什么我无法从Windows窗体保存.

I am trying to save a record BOOK_NAME value "Mit''in gizli tarihi" in this value it gives error "In operator without () in query expression "Mit''in......(''. " while saving record. Because of operator " '' "

But I can save this record from directly mdb file. why I can''t save from windows form.

string query = "Insert INTO BOOKS(BOOK_NAME,TYPE,AUTHOR,PUBLISH_YEAR,PUBLISH_COMPANY,INFO) VALUES (''" + txtBookName.Text  + "'', ''"
                    + comboTypes.Text + "'', ''" +  txtBookAuthor.Text+ "'', ''" +txtPunlishYear.Text  + "'', ''" +txtPublishCo.Text  + "'',''"+ txtInfo.Text +"'' )";

OleDbCommand Command = new OleDbCommand(query, dbConnection);

 if (dbConnection.State.ToString() == "Closed")
                    dbConnection.Open();
Command.ExecuteReader();

dbConnection.Close();

推荐答案

使用
Use parameters[^] instead of string concatenation.

Regards
Espen Harlinn


问题是BOOK_NAME中带有撇号(单引号),这是SQL字符串定界符,导致插入的其余部分未对齐.要避免默认行为,您需要在插入数据库中的任何字符串上用两个单引号''替换单引号".例如:
The problem is that the BOOK_NAME has an apostrophe (single quote) in it, which is the SQL string delimiter, causing the rest of your insert to be misaligned. To escape the default behavior you need to replace the single quote '' with two single quotes '''' on any strings you insert into the database. For example:
"... ''" + txtBookName.Text.Replace("''", "''''") + "'' ..."


但是,使用Espen Harlinn推荐的参数会更安全,并且会自动为您完成此操作.


However, using parameters as Espen Harlinn recommended, is safer and will do this automatically for you.


谢谢大家.

@ JOAT-MON

这是一个非常快速的修复.

@Espen Harlinn

谢谢.

我将研究参数.
Thank you guys.

@JOAT-MON

this is a very quick fix.

@Espen Harlinn

Thank you.

I will study about Parameters.


这篇关于C#Windows MDB操作员窗体保存问题记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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