为什么我需要OleDbCommand.Prepare()?

时间:2021-09-17 22:29:25

I'm working with a datagrid and adapter that correspond with an MSAccess table through a stored query (named "UpdatePaid", 3 paramaters as shown below) like so:

我正在使用数据网格和适配器,它通过存储的查询(名为“UpdatePaid”,3个参数,如下所示)与MSAccess表对应,如下所示:

    OleDbCommand odc = new OleDbCommand("UpdatePaid", connection);

    OleDbParameter param;

    odc.CommandType = CommandType.StoredProcedure;

    param = odc.Parameters.Add("v_iid", OleDbType.Double);
    param.SourceColumn = "I";
    param.SourceVersion = DataRowVersion.Original;

    param = odc.Parameters.Add("v_pd", OleDbType.Boolean);
    param.SourceColumn = "Paid";
    param.SourceVersion = DataRowVersion.Current;

    param = odc.Parameters.Add("v_Projected", OleDbType.Currency);
    param.SourceColumn = "ProjectedCost";
    param.SourceVersion = DataRowVersion.Current;

    odc.Prepare();

    myAdapter.UpdateCommand = odc;

    ...

    myAdapter.Update();

It works fine...but the really weird thing is that it didn't until I put in the odc.Prepare() call.

My question is thus: Do I need to do that all the time when working with OleDb stored procs/queries? Why? I also have another project coming up where I'll have to do the same thing with a SqlDbCommand... do I have to do it with those, too?

它工作正常......但真正奇怪的是它直到我输入odc.Prepare()调用。我的问题是:在使用OleDb存储过程/查询时,我是否需要一直这样做?为什么?我还有另一个项目即将到来,我将不得不用SqlDbCommand做同样的事情......我也必须用这些做吗?

3 个解决方案

#1


3  

This is called, oddly enough, a prepared statement, and they're actually really nice. Basically what happens is you either create or get a sql statement (insert, delete, update) and instead of passing actual values, you pass "?" as a place holder. This is all well and good, except what we want is our values to get passed in instead of the "?".

奇怪的是,这是一个准备好的声明,它们实际上非常好。基本上发生的是你创建或获取一个sql语句(插入,删除,更新)而不是传递实际值,你传递“?”作为占位符。这一切都很好,除了我们想要的是我们的值传递而不是“?”。

So we prepare the statement so instead of "?", we pass in parameters as you have above that are going to be the values that go in in place of the place holders.

所以我们准备语句而不是“?”,我们传递的参数就像你上面那样将是代替占位符的值。

Preparing parses the string to find where parameters can replace the question marks so all you have to do is enter the parameter data and execute the command.

准备解析字符串以查找参数可以替换问号的位置,这样您只需输入参数数据并执行命令即可。

Within oleDB, stored queries are prepared statements, so a prepare is required. I've not used stored queries with SqlDB, so I'd have to defer to the 2 answers previous.

在oleDB中,存储的查询是预处理语句,因此需要准备。我没有使用存储的查询与SqlDB,所以我不得不遵循以前的2个答案。

#2


0  

I don't use it with SqlDbCommand. It seems as a bug to me that it's required. It should only be nice to have if you're going to call a procedure multiple times in a row. Maybe I'm wrong and there's a note in documentation about providers that love this call too much.

我不使用它与SqlDbCommand。对我来说这似乎是一个需要它的错误。如果你要连续多次调用一个过程,那应该是很好的。也许我错了,在关于过度喜欢这个电话的提供商的文档中有一个注释。

#3


0  

Are you using the JET OLEDB Provider? or MSDASQL + JET ODBC?

您使用的是JET OLEDB Provider吗?还是MSDASQL + JET ODBC?

You should not need to call Prepare(), but I believe that's driver/provider dependent.

你不应该打电话给Prepare(),但我相信这依赖于驱动程序/提供者。

You definitely don't need to use Prepare() for System.Data.SqlClient.

您绝对不需要为System.Data.SqlClient使用Prepare()。

#1


3  

This is called, oddly enough, a prepared statement, and they're actually really nice. Basically what happens is you either create or get a sql statement (insert, delete, update) and instead of passing actual values, you pass "?" as a place holder. This is all well and good, except what we want is our values to get passed in instead of the "?".

奇怪的是,这是一个准备好的声明,它们实际上非常好。基本上发生的是你创建或获取一个sql语句(插入,删除,更新)而不是传递实际值,你传递“?”作为占位符。这一切都很好,除了我们想要的是我们的值传递而不是“?”。

So we prepare the statement so instead of "?", we pass in parameters as you have above that are going to be the values that go in in place of the place holders.

所以我们准备语句而不是“?”,我们传递的参数就像你上面那样将是代替占位符的值。

Preparing parses the string to find where parameters can replace the question marks so all you have to do is enter the parameter data and execute the command.

准备解析字符串以查找参数可以替换问号的位置,这样您只需输入参数数据并执行命令即可。

Within oleDB, stored queries are prepared statements, so a prepare is required. I've not used stored queries with SqlDB, so I'd have to defer to the 2 answers previous.

在oleDB中,存储的查询是预处理语句,因此需要准备。我没有使用存储的查询与SqlDB,所以我不得不遵循以前的2个答案。

#2


0  

I don't use it with SqlDbCommand. It seems as a bug to me that it's required. It should only be nice to have if you're going to call a procedure multiple times in a row. Maybe I'm wrong and there's a note in documentation about providers that love this call too much.

我不使用它与SqlDbCommand。对我来说这似乎是一个需要它的错误。如果你要连续多次调用一个过程,那应该是很好的。也许我错了,在关于过度喜欢这个电话的提供商的文档中有一个注释。

#3


0  

Are you using the JET OLEDB Provider? or MSDASQL + JET ODBC?

您使用的是JET OLEDB Provider吗?还是MSDASQL + JET ODBC?

You should not need to call Prepare(), but I believe that's driver/provider dependent.

你不应该打电话给Prepare(),但我相信这依赖于驱动程序/提供者。

You definitely don't need to use Prepare() for System.Data.SqlClient.

您绝对不需要为System.Data.SqlClient使用Prepare()。