c# 操作ACCESS数据库

 c# 简单操作ACCESS数据库

这两天做项目,需要将数据存到数据库,并进行一些简单的增删改操作,刚好记录下Access数据库的基本操作方法,下文中提及到知识应用如有不正确的地方,大家多多指正,微笑

在程序开始时,我打算使用一些数据库操作语句来创建一个数据库,不过好像用的不是很成功。而且如果要手动创建数据库,则在启动程序时,要判断没有某张表时,要创建该表。

1.首先要连接数据库:

其中的SewWorkStation.accdb为项目中要用到的数据库。
  1. public static string dbPath = System.Windows.Forms.Application.StartupPath + "\\SewWorkStation.accdb";
  2. public string dbName = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+dbPath+ ";";
  3. public OleDbConnection oleDbConn = null;
  4. public DBOperate()
  5. {
  6. oleDbConn = new OleDbConnection(dbName);
  7. }

2.用语句创建表:

  
  1. public void CreateRobotTable( )
  2. {
  3. try
  4. {
  5. oleDbConn.Open();
  6. string excuteStr = "Create Table t_Robot (rId int ,rName text,rIP text,rPort text)";
  7. OleDbCommand oleDbComm = new OleDbCommand(excuteStr, oleDbConn);
  8. oleDbComm.ExecuteNonQuery();
  9. }
  10. catch (Exception e)
  11. {
  12. MessageBox.Show(e.Message);
  13. }
  14. finally
  15. {
  16. oleDbConn.Close();
  17. }
  18. }
当然也可以在外部建立好数据库建好表,然后在程序中只进行增删改操作,这样也不用检查表是否存在了。
大家可能看到我这里创建表的属性,与下面查询时表的属性不一致哈~,其实我是在外部创建好一个空的表的,这里面只是记录下语法啦。

在网上查到了一段代码,判断数据库中是否存在某表,不过我执行了几次,就是刚创建了表也找不到,dtTable中是空的,大家如果有什么好的方法可以提醒我一下,微笑
  1. public bool VerifyTableInAccess( string TableName)
  2. {
  3. bool flag = false;
  4. try
  5. {
  6. oleDbConn.Open();
  7. DataTable dtTable = oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, TableName });
  8. if (dtTable == null)
  9. {
  10. flag = false;
  11. return flag;
  12. }
  13. foreach (DataRow DRow in dtTable.Rows)
  14. {
  15. if (DRow[ "TABLE_NAME"].ToString().Trim().ToUpper() == TableName.Trim().ToUpper())
  16. {
  17. flag = true;
  18. break;
  19. }
  20. }
  21. }
  22. catch (Exception e)
  23. {
  24. MessageBox.Show(e.Message);
  25. flag = false;
  26. }
  27. finally
  28. {
  29. oleDbConn.Close();
  30. }
  31. return flag;
  32. }


3.预览表:

 
  1. public void ShowTable( string tableName,DataGridView dataGridView )
  2. {
  3. try
  4. {
  5. oleDbConn.Open();
  6. DataSet dataSet = new DataSet();
  7. OleDbDataAdapter adapter = new OleDbDataAdapter();
  8. OleDbCommand command = new OleDbCommand( "select * from " + tableName, oleDbConn);
  9. adapter.SelectCommand = command;
  10. adapter.Fill(dataSet);
  11. dataGridView.DataSource = dataSet.Tables[ 0];
  12. }
  13. catch (Exception e)
  14. {
  15. MessageBox.Show(e.Message);
  16. }
  17. finally
  18. {
  19. oleDbConn.Close();
  20. }
  21. }
这里面使用到了适配器OleDbDataAdapter,也可以使用OleDbDataReader来读取。

4.添加数据:

  1. public void Insert(string table, object obj )
  2. {
  3. string insertStr = "";
  4. try
  5. {
  6. oleDbConn.Open();
  7. OleDbCommand oleDbComm = null;
  8. switch (table)
  9. {
  10. case "t_Robot":
  11. MRobot robot = (MRobot)obj;
  12. insertStr = "Insert into t_Robot(机器人编号,机器人名称,机器人IP,机器人端口) Values(?,?,?,?)";
  13. oleDbComm = new OleDbCommand(insertStr, oleDbConn);
  14. oleDbComm.Parameters.AddWithValue( "机器人编号", robot.Id);
  15. oleDbComm.Parameters.AddWithValue( "机器人名称", robot.Name);
  16. oleDbComm.Parameters.AddWithValue( "机器人IP", robot.IP);
  17. oleDbComm.Parameters.AddWithValue( "机器人端口", robot.Port);
  18. break;
  19. default:
  20. break;
  21. }
  22. if (! "".Equals(insertStr))
  23. {
  24. oleDbComm.ExecuteNonQuery();
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. MessageBox.Show(e.Message);
  30. }
  31. finally
  32. {
  33. oleDbConn.Close();
  34. }
这里要添加数据,就需要参数化查询的方式,使用OleDbCommand.Parameters这个属性了。

5.删除数据:

通过id来删除数据,开始时使用了Access中自动的Id,但发现即使删了一条数据后,后面数据的id没有自动更新,如果再新添加一条数据,id是max(id)+1,这点要注意。
  1. public void Delete(string table,int id )
  2. {
  3. string delStr = "";
  4. string paramName = "";
  5. try
  6. {
  7. switch(table)
  8. {
  9. case "t_Robot":
  10. delStr = "Delete * from t_Robot where 机器人编号=?";
  11. paramName = "rId";
  12. break;
  13. default:
  14. break;
  15. }
  16. if(! "".Equals(delStr))
  17. {
  18. oleDbConn.Open();
  19. OleDbCommand oleDbComm = new OleDbCommand(delStr, oleDbConn);
  20. oleDbComm.Parameters.AddWithValue(paramName, id);
  21. oleDbComm.ExecuteNonQuery();
  22. }
  23. }
  24. catch (Exception e)
  25. {
  26. MessageBox.Show(e.Message);
  27. }

6.更新删除数据之后数据的id:

  1. public void UpdateId(string table ,int id )
  2. {
  3. string updateStr = "";
  4. try
  5. {
  6. oleDbConn.Open();
  7. OleDbCommand oleDbComm = null;
  8. switch (table)
  9. {
  10. case "t_Robot":
  11. updateStr = "update t_Robot set 机器人编号 = 机器人编号-1 where 机器人编号>@index";
  12. break;
  13. default:
  14. break;
  15. }
  16. if (! "".Equals(updateStr))
  17. {
  18. oleDbComm = new OleDbCommand(updateStr, oleDbConn);
  19. oleDbComm.Parameters.AddWithValue( "@index",id);
  20. oleDbComm.ExecuteNonQuery();
  21. }
  22. }
  23. catch (Exception e)
  24. {
  25. MessageBox.Show(e.Message);
  26. }
  27. finally
  28. {
  29. oleDbConn.Close();
  30. }
  31. }
注意喔,上面的更新语句中是update  tableName set id = id -1 where id > delId ; 看到没里面没有select...,Access的更新语句与sql的update语句不同。

7.修改数据

  1. public void Update(string table ,object obj )
  2. {
  3. string updateStr = "";
  4. try
  5. {
  6. oleDbConn.Open();
  7. OleDbCommand oleDbComm = null;
  8. switch (table)
  9. {
  10. case "t_Robot":
  11. MRobot robot = (MRobot)obj;
  12. updateStr = "update t_Robot set 机器人名称=?,机器人IP=?,机器人端口=? where 机器人编号=?";
  13. oleDbComm = new OleDbCommand(updateStr, oleDbConn);
  14. oleDbComm.Parameters.AddWithValue( "机器人名称", robot.Name);
  15. oleDbComm.Parameters.AddWithValue( "机器人IP", robot.IP);
  16. oleDbComm.Parameters.AddWithValue( "机器人端口", robot.Port);
  17. oleDbComm.Parameters.AddWithValue( "机器人编号", robot.Id);
  18. break;
  19. default:
  20. break;
  21. }
  22. if(! "".Equals(updateStr))
  23. {
  24. oleDbComm.ExecuteNonQuery();
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. MessageBox.Show(e.Message);
  30. }
  31. finally
  32. {
  33. oleDbConn.Close();
  34. }
  35. }


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值