Home  >  Article  >  Database  >  C#中access数据库操作问题

C#中access数据库操作问题

WBOY
WBOYOriginal
2016-06-07 15:38:341183browse

在执行UPDATE和INSERT语句时,需要传入参数,例如这个dal层的 操作 : 1 public static void ModifyUserList(UserList userList) 2 { 3 string OleDb = " UPDATE " + " UserList " + " SET " + 4 " [UserName]=@UserName, " + 5 " [Password]=@Password " +

在执行UPDATE和INSERT语句时,需要传入参数,例如这个dal层的操作

<span> 1</span> <span>public</span> <span>static</span> <span>void</span> ModifyUserList(UserList userList)<br><span> 2</span>         {<br><span> 3</span>             <span>string</span> OleDb =<span>"</span><span>UPDATE </span><span>"</span>+<span>"</span><span>UserList </span><span>"</span>+<span>"</span><span>SET </span><span>"</span>+<br><span> 4</span>                     <span>"</span><span>[UserName]=@UserName, </span><span>"</span>+<br><span> 5</span>                     <span>"</span><span>[Password]=@Password </span><span>"</span>+<br><span> 6</span>                     <span>"</span><span> WHERE [ID]=@ID</span><span>"</span>;<br><span> 7</span>             <span>try</span><br><span> 8</span>             {<br><span> 9</span>                 OleDbParameter[] para = <span>new</span> OleDbParameter[]<br><span>10</span>                 {<br><span>11</span>                     <br><span>12</span>                     <span>new</span> OleDbParameter(<span>"</span><span>@UserName</span><span>"</span>,userList.UserName),<br><span>13</span>                     <span>new</span> OleDbParameter(<span>"</span><span>@Password</span><span>"</span>,userList.Password),<br><span>14</span>                     <span>new</span> OleDbParameter(<span>"</span><span>@ID</span><span>"</span>,userList.ID)<br><span>15</span>                     <br><span>16</span>                 };<br><span>17</span>                 DBHelper.ExecuteCommand(OleDb, para);<br><span>18</span>             }<br><span>19</span>             <span>catch</span> (Exception e)<br><span>20</span>             {<br><span>21</span>                 Console.WriteLine(e.Message);<br><span>22</span>                 <span>throw</span> e;<br><span>23</span>             }<br><span>24</span>         }        

起初insert和update的语句都不成功,抛出异常说语法错误,后来在网上看了一些网友的提问和回答,程序中存在这两个错误:

1.字段名与ACCESS关键字冲突,需要在字段名前加方括号“[]”(导致程序抛出异常)

2.参数顺序不对,ACCESS要求参数组中参数的顺序和sql语句中参数出现的顺序一致(程序会正常执行,但是语句执行返回的影响行数却总是0)

 

我起初按1修改,程序不再抛出异常但是执行后数据库没有变化,然后我又按2调整了参数的顺序,顺利修改了数据库的值。

 

我的问题是:

1.我的表字段的三个名字是ID,Password,UserName,我查了ACCESS的保留字,这三个名字都不是关键字为什么还会抛出异常?

2.关于ACCESS要求参数组中参数的顺序和sql语句中参数出现的顺序一致这个问题,是ACCESS数据库的特殊限制,还是微软的.NET类库在ACCESS访问这方面没有做好??

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn