c#访问access 数据库
来源:互联网 发布:淘宝美工注意事项 编辑:程序博客网 时间:2024/05/13 10:11
using System.Collections;
using System;
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using CMMS_SVR.Event;
public class AccessData {
public DataTable dtYourData;
public string datapath ;
// private static string user = ";Jet OLEDB:Database Password=ccrbcn;";
private string con ;
private OleDbConnection oCon;
public IList mycopy_colunm_name = new ArrayList();
private IList mycopy_colunm_type = new ArrayList();
public AccessData()
{
datapath = CMMS_SVR.Properties.Settings.Default._datapath;//"C:\\cmms.mdb";
// con = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" + datapath + ";";
con =//"Driver={Microsoft Access Driver (*.mdb)};"+
"Provider=Microsoft.Jet.OLEDB.4.0;"+
"Data Source=" + datapath +
//";UID=管理员;PassWord=sa;"+
";Persist Security Info=False;Jet OLEDB:Database Password=" + CMMS_SVR.Properties.Settings.Default._datapass;
}
public AccessData(string datapath,string datapass)
{
this.datapath = datapath;
con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + datapath +
//";UID=管理员;PassWord=sa;"+
";Persist Security Info=False;Jet OLEDB:Database Password=" + CMMS_SVR.Properties.Settings.Default._datapass;
}
public Boolean MyUpdatasql(string strsql)///////////跟新数据库
{
oCon = new OleDbConnection(con);
OleDbCommand oCmd = new OleDbCommand(strsql, oCon);
try
{
oCon.Open();
// lets use a datareader to fill that table!
oCmd.ExecuteNonQuery();
oCon.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
return false;
}
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
return true;
//Debug.Log(dtYourData.Rows[0]["work_1"].ToString());
}
public void myselect(string strsql, string tableName)
{
oCon = new OleDbConnection(con);
OleDbCommand oCmd = new OleDbCommand(strsql, oCon);
dtYourData = null;
dtYourData = new DataTable(tableName);
try
{
// open the connection
oCon.Open();
// lets use a datareader to fill that table!
OleDbDataReader rData = oCmd.ExecuteReader();
// now lets blast that into the table by sheer man power!
dtYourData.Load(rData);
// close that reader!
rData.Close();
// close your connection to the spreadsheet!
oCon.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
}
}
public void inser_or_delete(string strsql)
{
oCon = new OleDbConnection(con);
OleDbCommand oCmd = new OleDbCommand(strsql, oCon);
try
{
// open the connection
oCon.Open();
// lets use a datareader to fill that table!
oCmd.ExecuteNonQuery();
// now lets blast that into the table by sheer man power!
// close your connection to the spreadsheet!
oCon.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
}
}
internal void myselect(string sql1)
{
throw new NotImplementedException();
}
public DataSet select(string sql)
{
DataSet dsReturn = new DataSet();
OleDbConnection conn = new OleDbConnection(con);
try
{
conn.Open();
OleDbDataAdapter comm = new OleDbDataAdapter(sql, conn);
comm.Fill(dsReturn);
conn.Close();
}
catch (Exception ex)
{
conn.Close();
Console.WriteLine("数据库操作失败,错误打印:"+ex.ToString());
}
{
return dsReturn;
}
}
public Boolean deleteTable(string tablename)
{
oCon = new OleDbConnection(con);
OleDbCommand oCmd = new OleDbCommand("drop table " + tablename, oCon);
try
{
oCon.Open();
// lets use a datareader to fill that table!
oCmd.ExecuteNonQuery();
oCon.Close();
}
catch (Exception ex)
{
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
return false;
}
if (oCon.State != ConnectionState.Closed)
oCon.Close();
oCon.Dispose();
return true;
}
public Boolean manyInsert(string sqlTableName,DataSet mydataset)
{
sqlTableName += "select * From " + sqlTableName + " ";
DataSet mydataset1 = new AccessData().select(sqlTableName);
if (mydataset1.Tables.Count == 0 )
{
EventTool.createAccessTable(EventTool.createAccessMaterialPlan);
}
OleDbConnection conn = new OleDbConnection(con);
try
{
conn.Open();
OleDbDataAdapter comm = new OleDbDataAdapter(sqlTableName, conn);
OleDbCommandBuilder cmdbld = new OleDbCommandBuilder(comm);
cmdbld.SetAllValues = true;
comm.InsertCommand = cmdbld.GetInsertCommand();
mydataset.Tables[0].BeginLoadData();
int rowcount = mydataset.Tables[0].Rows.Count;
for (int n = 0; n < rowcount; n++)
{
mydataset.Tables[0].Rows[n].SetAdded();
}
mydataset.Tables[0].EndLoadData();
comm.UpdateBatchSize = 2000;
comm.Update(mydataset, mydataset.Tables[0].TableName);
comm.Dispose();
conn.Close();
conn.Dispose();
mydataset.Dispose();
}
catch (Exception ex)
{
conn.Close();
conn.Dispose();
return false;
Console.WriteLine("数据库操作失败,错误打印:" + ex.ToString());
}
{
return true;
}
}
}
0 0
- c# 访问Access数据库
- C#访问Access数据库
- c#访问access 数据库
- 用C#访问Access数据库
- C#远程访问Access数据库
- 用C#访问Access数据库
- 用C#访问Access数据库
- 用C#访问Access数据库
- C#访问操作Access数据库
- 用C#访问Access数据库 (转)
- 用C#访问ACCESS数据库问题
- C#访问带密码的ACCESS数据库
- C# 访问 带密码的access数据库
- c#访问带有密码的Access数据库(我是c#初学者)
- C#访问Access数据库的数据库操作类
- C#2005 一个简单的Access数据库访问类
- ASP.NET C# 访问Access、Xml等数据库
- C# .NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
- java中HashMap详解
- [EXtJS5学习笔记]第一节 Sencha Cmd 学习笔记 简介 Sencha Cmd是什么
- PGIS平台部署中的问题及解决方案
- Oracle11g启动关闭脚本
- jQuery选择器总结
- c#访问access 数据库
- 3 定义两个整型指针,分别用malloc、calloc对其分配空间保存3个元素,malloc分配的空间用memset清零,随机对数组进行赋值随机范围1-3,赋值后用memcmp比较两个数组。如果相同
- 一个简单的计算获得的日期与现在的日期差
- CP&SP
- Android Bug
- 修改MySQL数据库端口号 2.0
- openstack windows下远程debug调试(komodo)
- svn 更换IP eclipse 提交没有变更前的代码
- c#调用c语言dll,形参有char*