我在 C#/.net 中使用 OleDB,不喜欢 oledb 使用问号处理参数的方式:
using (OleDbConnection sqlConnect = drtevs.GetConnection())
{
OleDbCommand command.CommandText = "UPDATE NAME SET FIRST_NAME = ?, LAST_NAME = ? WHERE ID = ?;";
command.Parameters.Add(new OleDbParameter("@FIRST_NAME", txtFirst));
command.Parameters.Add(new OleDbParameter("@LAST_NAME", txtLast));
command.Parameters.Add(new OleDbParameter("@ID", 12));
command.ExecuteNonQuery();
}
我看到的问题是它们没有命名,因此不能重用它们。有没有更好的方法用 OleDB 做参数?如果有人能给我代码来执行类似 LINQ-TO-SQL 的代码以下方式:
db.ExecuteQuery("UPDATE NAME SET FIRST_NAME = {0}, LAST_NAME = {1} WHERE ID = {2}", txtFirst, txtLast, 12);
我知道 native SQL .net 提供程序具有命名参数(例如 @FIRST_NAME、@LAST_NAME),但由于我同时使用 SQL Server 和 Oracle,所以这不是一个选项。
编辑: 我想通过清理我的参数来避免 SQL 注入(inject),并且还能够插入不可序列化的对象,如字节数组。
如果我的姓氏是“' DROP TABLE NAME --”,则到目前为止给出的所有答案都将失败,此外,它还会对我的数据库造成损坏
最佳答案
如果你想重新使用这个值,并且你想要理智的查询,也许在道德上等同于:
declare @id int = ?
declare @when datetime = ?
...
// some complex query involving @id and @when
这里?
限定在最上面,上下文很清楚。
关于c# - 解析 oledb 的参数化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6903408/