c# 简单操作ACCESS数据库
这两天做项目,需要将数据存到数据库,并进行一些简单的增删改操作,刚好记录下Access数据库的基本操作方法,下文中提及到知识应用如有不正确的地方,大家多多指正,
。
在程序开始时,我打算使用一些数据库操作语句来创建一个数据库,不过好像用的不是很成功。而且如果要手动创建数据库,则在启动程序时,要判断没有某张表时,要创建该表。
1.首先要连接数据库:
其中的SewWorkStation.accdb为项目中要用到的数据库。
- public static string dbPath = System.Windows.Forms.Application.StartupPath + "\\SewWorkStation.accdb";
- public string dbName = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+dbPath+";";
- public OleDbConnection oleDbConn = null;
- public DBOperate()
- {
- oleDbConn = new OleDbConnection(dbName);
- }
2.用语句创建表:
- public void CreateRobotTable( )
- {
- try
- {
- oleDbConn.Open();
- string excuteStr = "Create Table t_Robot (rId int ,rName text,rIP text,rPort text)";
- OleDbCommand oleDbComm = new OleDbCommand(excuteStr, oleDbConn);
- oleDbComm.ExecuteNonQuery();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- oleDbConn.Close();
- }
- }
大家可能看到我这里创建表的属性,与下面查询时表的属性不一致哈~,其实我是在外部创建好一个空的表的,这里面只是记录下语法啦。
在网上查到了一段代码,判断数据库中是否存在某表,不过我执行了几次,就是刚创建了表也找不到,dtTable中是空的,大家如果有什么好的方法可以提醒我一下,
。
。- public bool VerifyTableInAccess( string TableName)
- {
- bool flag = false;
- try
- {
- oleDbConn.Open();
- DataTable dtTable = oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, TableName });
- if (dtTable == null)
- {
- flag = false;
- return flag;
- }
- foreach (DataRow DRow in dtTable.Rows)
- {
- if (DRow["TABLE_NAME"].ToString().Trim().ToUpper() == TableName.Trim().ToUpper())
- {
- flag = true;
- break;
- }
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- flag = false;
- }
- finally
- {
- oleDbConn.Close();
- }
- return flag;
- }
3.预览表:
- public void ShowTable( string tableName,DataGridView dataGridView )
- {
- try
- {
- oleDbConn.Open();
- DataSet dataSet = new DataSet();
- OleDbDataAdapter adapter = new OleDbDataAdapter();
- OleDbCommand command = new OleDbCommand("select * from " + tableName, oleDbConn);
- adapter.SelectCommand = command;
- adapter.Fill(dataSet);
- dataGridView.DataSource = dataSet.Tables[0];
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- oleDbConn.Close();
- }
- }
4.添加数据:
- public void Insert(string table, object obj )
- {
- string insertStr = "";
- try
- {
- oleDbConn.Open();
- OleDbCommand oleDbComm = null;
- switch (table)
- {
- case "t_Robot":
- MRobot robot = (MRobot)obj;
- insertStr = "Insert into t_Robot(机器人编号,机器人名称,机器人IP,机器人端口) Values(?,?,?,?)";
- oleDbComm = new OleDbCommand(insertStr, oleDbConn);
- oleDbComm.Parameters.AddWithValue("机器人编号", robot.Id);
- oleDbComm.Parameters.AddWithValue("机器人名称", robot.Name);
- oleDbComm.Parameters.AddWithValue("机器人IP", robot.IP);
- oleDbComm.Parameters.AddWithValue("机器人端口", robot.Port);
- break;
- default:
- break;
- }
- if (!"".Equals(insertStr))
- {
- oleDbComm.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- oleDbConn.Close();
- }
5.删除数据:
通过id来删除数据,开始时使用了Access中自动的Id,但发现即使删了一条数据后,后面数据的id没有自动更新,如果再新添加一条数据,id是max(id)+1,这点要注意。
- public void Delete(string table,int id )
- {
- string delStr = "";
- string paramName = "";
- try
- {
- switch(table)
- {
- case "t_Robot":
- delStr = "Delete * from t_Robot where 机器人编号=?";
- paramName = "rId";
- break;
- default:
- break;
- }
- if(!"".Equals(delStr))
- {
- oleDbConn.Open();
- OleDbCommand oleDbComm = new OleDbCommand(delStr, oleDbConn);
- oleDbComm.Parameters.AddWithValue(paramName, id);
- oleDbComm.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
6.更新删除数据之后数据的id:
- public void UpdateId(string table ,int id )
- {
- string updateStr = "";
- try
- {
- oleDbConn.Open();
- OleDbCommand oleDbComm = null;
- switch (table)
- {
- case "t_Robot":
- updateStr = "update t_Robot set 机器人编号 = 机器人编号-1 where 机器人编号>@index";
- break;
- default:
- break;
- }
- if (!"".Equals(updateStr))
- {
- oleDbComm = new OleDbCommand(updateStr, oleDbConn);
- oleDbComm.Parameters.AddWithValue("@index",id);
- oleDbComm.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- oleDbConn.Close();
- }
- }
注意喔,上面的更新语句中是update tableName set id = id -1 where id > delId ; 看到没里面没有select...,Access的更新语句与sql的update语句不同。
7.修改数据
- public void Update(string table ,object obj )
- {
- string updateStr = "";
- try
- {
- oleDbConn.Open();
- OleDbCommand oleDbComm = null;
- switch (table)
- {
- case "t_Robot":
- MRobot robot = (MRobot)obj;
- updateStr = "update t_Robot set 机器人名称=?,机器人IP=?,机器人端口=? where 机器人编号=?";
- oleDbComm = new OleDbCommand(updateStr, oleDbConn);
- oleDbComm.Parameters.AddWithValue("机器人名称", robot.Name);
- oleDbComm.Parameters.AddWithValue("机器人IP", robot.IP);
- oleDbComm.Parameters.AddWithValue("机器人端口", robot.Port);
- oleDbComm.Parameters.AddWithValue("机器人编号", robot.Id);
- break;
- default:
- break;
- }
- if(!"".Equals(updateStr))
- {
- oleDbComm.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- oleDbConn.Close();
- }
- }
本文介绍了如何使用C#语言操作Access数据库,包括连接数据库、创建表、预览表、添加数据、删除数据、更新数据等基本操作。文章还提供了一些实用的代码示例。

313

被折叠的 条评论
为什么被折叠?



