Access数据库不保存数据 [英] Access database doesn't save data

查看:246
本文介绍了Access数据库不保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

salam

我试着使用这个代码将数据插入到访问数据库中

当我点击添加按钮时它显示记录已添加但当我关闭时应用程序并重新打开它我发现数据库空了

这里有数据库无法保存数据的问题



什么我试过了:



  string  chem = < span class =code-string> @  Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \ MyDB.accdb; Persist Security Info = False;; OleDbConnection conx =  new  OleDbConnection(chem); 
conx.Open(); OleDbCommand insert = new OleDbCommand();
insert.Connection = conx;
insert.CommandText = 插入[User](Usr,Pass,Profil,Etat,Obsrv)值(' + textBox46.Text + ',' + pass + ',' + comboBox11.Text.ToString()+ ','نشط','لاشيئ');

insert.ExecuteNonQuery();

MessageBox.Show( L'utilisateur' + textBox46.Text + 'aétèajoutéavecsuccess 信息,MessageBoxButtons.OK,MessageBoxIcon.Information);
conx.Close();

解决方案

我找到了解决方案

我的MDB(ACCDB)文件包含在你的项目中文件。

当我检查与此项目文件相关的属性时,我发现一个名为



复制到输出目录设置为始终复制。



连接字符串也包含快捷方式



数据源=| DataDirectory | \db.mdb



所以解决方案



i启动我的应用程序并将MDB文件从原始位置复制到BIN \DEBUG目录。

i在那里插入数据没有错误。

i停止调试会话以修复错误或其他问题

i重新启动应用程序并且我的数据库的新副本(但是空的)将再次复制到输出目录中。

修复此问题,将属性设置为从不复制或将连接字符串更改为指向固定位置。



作为旁注:切勿使用字符串连接来构建sql命令。这将是一个巨大的安全风险(Sql注入攻击)和众多解析问题的来源(日期格式化,带单引号的字符串,非整数的小数分隔符)。使用ALWAYS参数化查询。


salam
i'm tryin to insert data into access database using this code
when i click add button it shows that the record has added but when i close the application and reopen it i found the data base empty
where is the probleme why the database doesn't save data

What I have tried:

string chem =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDB.accdb;Persist Security Info=False;";OleDbConnection conx = new OleDbConnection(chem);
          conx.Open();     OleDbCommand insert = new OleDbCommand();
              insert.Connection = conx;
              insert.CommandText = "insert into [User] (Usr,Pass,Profil,Etat,Obsrv) values('" + textBox46.Text + "','" + pass + "','" + comboBox11.Text.ToString() + "','نــــــــــــــشـــــــــط','لا شيئ')";

              insert.ExecuteNonQuery();

              MessageBox.Show("L'utilisateur '" + textBox46.Text + "' a étè ajouté avec succes", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
              conx.Close();

解决方案

i found the solution
my MDB (ACCDB) file included in your project files.
when i check the properties associated with this project file i found one named

Copy to Output Directory set to Copy Always.

Also the connection string contains the shortcut

Data Source = "|DataDirectory|\db.mdb"

so the solution

i start my application and the MDB file is copied from the original location to the BIN\DEBUG directory.
i insert data there without error.
i stop the debug session to fix errors or other problems
i restart the application and the fresh copy (but empty) of my db is copied again in the output directory.
Fix it setting the property to Copy Never or change connection string to point to a fixed location.

As a side note: NEVER use string concatenation to build your sql commands. This will be a huge security risk (Sql Injection Attacks) and the source of numerous parsing problems (Dates formatting, string with single quotes, decimal separator for non integer numbers). Use ALWAYS parametrized queries.


这篇关于Access数据库不保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆