<pre name="code" class="csharp"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; font-weight: normal; background-color: rgb(255, 255, 255);"><span style="font-size:24px;">c# 操作Access数据库</span></span>
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);
} 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();
}
}
当然也可以在外部建立好数据库建好表,然后在程序中只进行增删改操作,这样也不用检查表是否存在了。
。 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;
}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();
}
}
这里面使用到了适配器OleDbDataAdapter,也可以使用OleDbDataReader来读取。 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();
} 这里要添加数据,就需要参数化查询的方式,使用OleDbCommand.Parameters这个属性了。 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);
} 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();
}
}
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();
}
}原文地址:http://blog.csdn.net/nanxizhu/article/details/40865969