如何使用c#操作ACCESS数据库
如何使用c#操作ACCESS数据库: 手头没有Microsoft Access,如何建立数据库,一切依然简单. 首先引用C:Program FilesCommon FilesSystemadomsadox.dll,该DLL包含ADOX命名空间; 接着引用C:Program FilesCommon FilesSystemadomsjro.dll,该DLL包含JRO命名空间 SxS
如何使用c#操作ACCESS数据库:
手头没有Microsoft Access,如何建立数据库,一切依然简单.
首先引用C:Program FilesCommon FilesSystemadomsadox.dll,该DLL包含ADOX命名空间;
接着引用C:Program FilesCommon FilesSystemadomsjro.dll,该DLL包含JRO命名空间
SxS好问提示:如,导入dll不成功,手动把com组件导入为 .net组件,在用vs.net工具导入
using System;
using System.IO;
using ADOX; //该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll
using JRO; //该命名空间包含压缩ACCESS的类(方法)
public class Access
...{
/**////根据指定的文件名称创建ACCESS数据库
///mdbPath:要创件的ACCESS绝对路径
public void Create( string mdbPath )
...{
if( File.Exists(mdbPath) ) //检查数据库是否已存在
...{
throw new Exception("目标数据库已存在,无法创建");
}
// 可以加上密码,这样创建后的数据库必须输入密码后才能打开
mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
// 创建一个CatalogClass对象的实例,
ADOX.CatalogClass cat = new ADOX.CatalogClass();
// 使用CatalogClass对象的Create方法创建ACCESS数据库
cat.Create(mdbPath);
}
/**////压缩修复ACCESS数据库,mdbPath为数据库绝对路径
public void Compact( string mdbPath )
...{
if( !File.Exists(mdbPath) ) //检查数据库是否已存在
...{
throw new Exception("目标数据库不存在,无法压缩");
}
//声明临时数据库的名称
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = mdbPath.Substring(0, mdbPath.LastIndexOf("")+1) + temp;
//定义临时数据库的连接字符串
temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
//定义目标数据库的连接字符串
mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
//创建一个JetEngineClass对象的实例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
jt.CompactDatabase( mdbPath2, temp2 );
//拷贝临时数据库到目标数据库(覆盖)
File.Copy( temp, mdbPath, true );
//最后删除临时数据库
File.Delete( temp );
} /**//// 备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径
public void Backup( string mdb1, string mdb2 )
...{
if( !File.Exists(mdb1) )
...{
throw new Exception("源数据库不存在");
}
try
...{
File.Copy( mdb1, mdb2, true );
}
catch( IOException ixp )
...{
throw new Exception(ixp.ToString());
}
}
/**////恢复数据库,mdb1为备份数据库绝对路径,mdb2为当前数据库绝对路径
public void Recover( string mdb1, string mdb2 )
...{
if( !File.Exists(mdb1) )
...{
throw new Exception("备份数据库不存在");
}
try
...{
File.Copy( mdb1, mdb2, true );
}
catch( IOException ixp )
...{
throw new Exception(ixp.ToString());
}
}
}
******************************************************************************************************
在BETA2中,。NET提供了以下的NAMESPACE:
System.Data Namespace
System.Data.OleDb (和BETA1中已经不同了,所以如果拿BETA1中的程序到BETA2中来运行肯定不可以的)
如果想讲清楚这些东西,我不认为是我可以作到的,所以我想通过一些具体的程序来把我们对数据库的最基本的操作(SELECT、UPDATE、DELETE、INSERT等)演示一下,其他的还是需要朋友们在学习过程中来慢慢体会了!
要想操作一个数据库,,不论是那种操作,首先要做的肯定是打开数据库,下面我们以ACCESS数据库来做例子说明如何打开一个数据库连接!在这里我们需要用到的是:System.Data.OleDb.OleDbConnection类!(如果操作SQL数据库,我们最好使用 System.Data.SqlClient.SqlConnection类)
我先写出我自己使用的程序:
using System.Data
using System.Data.OleDb
public OleDbConnection getConn()
{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=F:webnotesbookclassleavenotes.mdb";
OleDbConnection tempconn= new OleDbConnection(connstr);
return(tempconn);
}
相信只要使用过ADO的朋友应该都可以看懂的!我们先定义一个String类型的变量,其中存放了我们连接数据库的连接字符串,然后在定义一个 System.Data.OleDb.OleDbConnection类型的对象并实例化,最后返回这个对象!需要说明一下的是,我并没有把语句: tempconn.Open();放到这个函数中,原因我我稍后在说明,这里只是先提醒一下!
通过上面的函数,我们就已经得到了类似于ADO中的连接对象Connection了!下面的就是具体操作数据库了!
在具体讲操作前,我认为有必要先认识一下下面的两个类:
System.Data.OleDb.OleDbDataAdapter
System.Data.OleDb.OleDbDataReader
System.Data.OleDb.OleDbDataAdapter:可以直接和DataSet联系,并操作数据源的,它的功能相对强大一些,因此也比较耗系统资源!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

The Nginx current limit problem can be solved by: use ngx_http_limit_req_module to limit the number of requests; use ngx_http_limit_conn_module to limit the number of connections; use third-party modules (ngx_http_limit_connections_module, ngx_http_limit_rate_module, ngx_http_access_module) to implement more current limit policies; use cloud services (Cloudflare, Google Cloud Rate Limiting, AWS WAF) to DD
