使用参数将数据插入access数据库
我有以下方法将数据插入到访问数据库中,该方法工作正常,但如果我尝试插入包含我学到的单引号的文本,我确实会遇到问题。
[WebMethod]
public void bookRatedAdd(string title, int rating, string review, string ISBN, string userName)
{
OleDbConnection conn;
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
Data Source=" + Server.MapPath("App_Data\\BookRateInitial.mdb"));
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = @"INSERT INTO bookRated([title], [rating], [review], [frnISBN], [frnUserName])VALUES('" + title + "', '" + rating + "','" + review + "','" + ISBN + "', '" + userName + "')";
cmd.ExecuteNonQuery();
conn.Close();
}
据我了解,解决问题的方法之一是使用参数。老实说,我不知道该怎么做。如何更改上述代码以便使用参数插入数据?
I have the following method to inserting data into an an access databasewhich works fine but I do get a problem if I try to insert text that contains single quotes I have learned.
[WebMethod]
public void bookRatedAdd(string title, int rating, string review, string ISBN, string userName)
{
OleDbConnection conn;
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
Data Source=" + Server.MapPath("App_Data\\BookRateInitial.mdb"));
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = @"INSERT INTO bookRated([title], [rating], [review], [frnISBN], [frnUserName])VALUES('" + title + "', '" + rating + "','" + review + "','" + ISBN + "', '" + userName + "')";
cmd.ExecuteNonQuery();
conn.Close();
}
From what I understand one of the ways to solve the problem is by using parameters. I am not sure how to do this to be honest. How could I change the above code so that I insert the data by using parameters instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与任何其他查询相同:
a) 替换 带有占位符的
OleDbCommand
(以@
为前缀),b) 添加
OleDbParameter
的实例 到DbCommand.Parameters
属性。参数名称必须与占位符名称匹配。Same as for any other query:
a) Replace actual hardcoded parameters in your
OleDbCommand
with placeholders (prefixed with@
),b) Add instances of
OleDbParameter
to theDbCommand.Parameters
property. Parameter names must match placeholder names.您必须使用参数来插入值。这也是一个安全问题。
如果你这样做,就可以进行sql注入。
尝试这样:
You have to use Parameter to insert Values. Its is allso a security Issue.
If you do it like that a sql injection could by made.
Try like this:
对于 Microsoft Access,参数是基于位置的且未命名,您应该使用
?
作为占位符符号,但如果您使用名称参数(只要它们的顺序相同),代码就可以工作。请参阅 OleDbCommand.Parameters 属性
请务必包含将使用参数的预期架构类型以及架构长度(如果适用)。
我还建议您始终在实例周围使用
using
语句,其中类型像OleDbConnection
一样实现IDisposable
,以便即使在代码中抛出异常。更改后的代码:
For Microsoft Access the parameters are positional based and not named, you should use
?
as the placeholder symbol although the code would work if you used name parameters provided they are in the same order.See the documentation for OleDbCommand.Parameters Property
Be sure to include the expected schema type where the parameter will be used AND the schema length if applicable.
I also recommend you always use
using
statements around your instances where the type implementsIDisposable
like theOleDbConnection
so that the connection is always closed even if an exception is thrown in the code.Changed Code: