首页  >  文章  >  数据库  >  .net用OLEDB方式操作SqlServer和Sybase

.net用OLEDB方式操作SqlServer和Sybase

WBOY
WBOY原创
2016-06-07 15:26:46975浏览

【错误一】 SqlServer提示错误:OleDbException 必须声明标量变量 今天用.net使用Oledb的方式操作SqlServer,却总是莫名其妙的报“必须声明标量变量…”,在代码里面分别采用了下面的两种方式,都无济于事: 方法一: OleDbParameter [] p = new OleDbParamete

【错误一】 SqlServer提示错误:OleDbException 必须声明标量变量

 

今天用.net使用Oledb的方式操作SqlServer,却总是莫名其妙的报“必须声明标量变量…”,在代码里面分别采用了下面的两种方式,都无济于事:

<span>方法一:</span>
<span>OleDbParameter</span>[] <span>p </span>= <span>new </span><span>OleDbParameter</span>[] {     <span>new </span><span>OleDbParameter</span>(<span>"@aa"</span>,1),    <span>new </span><span>OleDbParameter</span>(<span>"@bb"</span>,<span>"shelley"</span>),    <span>new </span><span>OleDbParameter</span>(<span>"@cc"</span>,<span>DateTime</span>.<span>Now</span>)};<span>Objcmd</span>.<span>Parameters</span>.<span>AddRange</span>(<span>p</span>);<span>方法二:</span>
<span>OleDbParameter </span><span>pp</span>;<span>pp </span>= <span>new </span><span>OleDbParameter</span>(<span>"@aa"</span>,<span>OleDbType</span>.<span>Integer</span>);<span>pp</span>.<span>Value </span>= 1;<span>Objcmd</span>.<span>Parameters</span>.<span>Add</span>(<span>pp</span>);<span>pp </span>= <span>new </span><span>OleDbParameter</span>(<span>"@bb"</span>, <span>OleDbType</span>.<span>LongVarChar</span>);<span>pp</span>.<span>Value </span>= <span>"shelley"</span>;<span>Objcmd</span>.<span>Parameters</span>.<span>Add</span>(<span>pp</span>);<span>pp </span>= <span>new </span><span>OleDbParameter</span>(<span>"@cc"</span>, <span>OleDbType</span>.<span>Date</span>);<span>pp</span>.<span>Value </span>= <span>DateTime</span>.<span>Now</span>;<span>Objcmd</span>.<span>Parameters</span>.<span>Add</span>(<span>pp</span>);
无论怎么样写,都会提示“必须声明标量变量@aa”,最终的原因是下面这句:
<span>OleDbCommand </span><span>Objcmd </span>= <span>new </span><span>OleDbCommand</span>(<span>"insert into test (a,b,c)values(@aa,@bb,@cc)"</span>, <span>Objconn</span>);
正确写法:
<span>OleDbCommand </span><span>Objcmd </span>= <span>new </span><span>OleDbCommand</span>(<span>"insert into test (a,b,c)values(?,?,?)"</span>, <span>Objconn</span>);
这样就正常通过了。
 
【错误二】Sybase提示错误:不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))

.net用Oledb操作Sybase,因为在一个sql语句中可能会多次用到同一个参数,代码如下:

<span>OleDbCommand </span><span>Objcmd </span>= <span>new </span><span>OleDbCommand</span>(<span>"update test set a=@aa where a=@aa"</span>, <span>Objconn</span>);<span>OleDbParameter</span>[] <span>p </span>= <span>new </span><span>OleDbParameter</span>[] {     <span>new </span><span>OleDbParameter</span>(<span>"aa"</span>,1),    <span>new </span><span>OleDbParameter</span>(<span>"aa"</span>,1)};<span>Objcmd</span>.<span>Parameters</span>.<span>AddRange</span>(<span>p</span>);<span>Objconn</span>.<span>Open</span>();<span>Objcmd</span>.<span>ExecuteNonQuery</span>();<span>Objconn</span>.<span>Close</span>();

上面的代码就会提示“不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))”错误。问题原因在于参数中存在两个相同名字,对于这种情况只需要写一个参数就可以了,改动如下:

<span>OleDbCommand </span><span>Objcmd </span>= <span>new </span><span>OleDbCommand</span>(<span>"update test set a=@aa where a=@aa"</span>, <span>Objconn</span>);<span>OleDbParameter</span>[] <span>p </span>= <span>new </span><span>OleDbParameter</span>[] {     <span>new </span><span>OleDbParameter</span>(<span>"aa"</span>,1)};

<span>OleDbCommand </span><span>Objcmd </span>= <span>new </span><span>OleDbCommand</span>(<span>"update test set a=@aa where a=@aa2"</span>, <span>Objconn</span>);<span>OleDbParameter</span>[] <span>p </span>= <span>new </span><span>OleDbParameter</span>[] {     <span>new </span><span>OleDbParameter</span>(<span>"aa"</span>,1),    <span>new </span><span>OleDbParameter</span>(<span>"aa2"</span>,1)};

【总结】

如果写一个程序,需要支持SqlServer和Sybase的时候这个地方需要特别注意:

1.SqlServer需要用“?”作为参数占位符,这样要求即使用到了同一个参数,也必须多次添加Parameter;

2.Sybase如果多次使用同一个参数,不能多次添加Parameter,否则会报错;

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn