C#操作Access数据库,收集了3篇经典文章
【导言】因为开发制作一个小工具数据转移工具,需要用C#操作Access数据库。所以上网搜索了一下,有三篇文章内容比较好,特收录如下。 另有一份大礼送给网友:《ADO 程序员参考》 另一个链接:CSDN下载-ADO程序员参考中文版 一、创建数据库的基本方法: 原作
【导言】因为开发制作一个小工具“数据转移工具”,需要用C#操作Access数据库。所以上网搜索了一下,有三篇文章内容比较好,特收录如下。
另有一份大礼送给网友:《ADO 程序员参考》
另一个链接:CSDN下载-ADO程序员参考中文版
一、创建数据库的基本方法:
原作者迭失
microsoft ado ext.2.8
我想利用C#创建一个ACCESS数据库文件(A.mdb)。请问用什么办法可以实现。
A.mdb文件是原来没有的,程序需要创建一个然后往里面写数据!
1.
※新建工程
※进入解决方案->引用->添加引用
->选择-> OK
※编码
//命令行工程代码如下
using System;
using ADOX;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\AccessDB\\NewMDB.mdb;" +
"Jet OLEDB:Engine Type=5");
Console.WriteLine("Database Created Successfully");
cat = null;
}
}
}
//asp.net代码如下
private void Page_Load(object sender, System.EventArgs e)
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C://database//NewMDB.mdb;" +
"Jet OLEDB:Engine Type=5");
cat = null;
Response.Write("OK");
2.用sqlserver 的代码如下
SqlConnection conn=new SqlConnection("Server=lemoncat007;Uid=sa;Pwd=gtt");
conn.Open();
SqlCommand cmd=new SqlCommand("create database test",conn);
cmd.ExecuteNonQuery();
3 也可以创建一个Procedure 将创建数据库的语句写到里面然后执行
二、创建数据库,并且添表和添加字段
原文《用C#动态创建Access数据库》
记得以前要动态的创建Access数据库的mdb文件都是采用DAO,用VC开发,一大堆的API,很是麻烦。现在好像也鲜有人提起DAO。其实动态的创建mdb数据的最简单的方法还是ADOX。
用ADOX创建access数据库方法很简单,只需要new一个Catalog对象,然后调用它的Create方法就可以了,如下:
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb;Jet OLEDB:Engine Type=5");
仅仅两行代码就搞定了。下来我主要介绍一下在c#中的实现细节。首先你要添加引用,在“Add reference”对话框里切换到Com页面,选择“Microsoft ADO Ext. 2.8 for DDL and Security”,然后点击OK。在文件的开头using ADOX名字空间。然后添加如上面所示的代码就可以成功的创建Access 数据库了,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using ADOX;
namespace testADOX
{
class Program
{
static void Main(string[] args)
{
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb;Jet OLEDB:Engine Type=5");
}
}
}
创建了数据库文件是没有实际用处的,我们还要创建表。在创建表之前,我们必须连接目标数据库,用来连接数据的桥梁居然是ADO的Connection对象,所以我们不得不再次添加对ADO的应用,在添加引用对话框中切换到Com页面,选择“Microsoft ActiveX Data Objects 2.8 Library”,然后点击OK。下边是创建表的完整代码:
using System;
using System.Collections.Generic;
using System.Text;
using ADOX;
namespace testADOX
{
class Program
{
static void Main(string[] args)
{
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb;Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb", null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = "FirstTable";
ADOX.Column column = new ADOX.Column();
column.ParentCatalog = catalog;
column.Name = "RecordId";
column.Type = DataTypeEnum.adInteger;
column.DefinedSize = 9;
column.Properties["AutoIncrement"].Value = true;
table.Columns.Append(column, DataTypeEnum.adInteger, 9);
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
catalog.Tables.Append(table);
cn.Close();
}
}
}
上面的代码中,创建了一个名为FirstTable的表,在表里加入了4个字段,并设置了一个主键。表里的字段分别输入4中不同的常用类型,第一个字段是一个自动增长的整数类型,这个类型比较特殊,你必须为这个字段设置ParentCatalog属性,并将“AutoIncrement”的属性值设为true.。Access里的Text类型对应的就是adVarWchar,而日期类型对应的是adDate。
键的设置如table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null)所示,如果是外键的话,你还必须要设置关联的表和关联的字段,也就是Append方法的后两个字段。
你也可以参照上边的代码创建索引和视图。
三、C#创建Access数据库的备注字段
原文:http://www.hackpig.cn/post/168.html
具体的用C#创建方式网上一堆,主要是创建备注字段的时候遇到的问题。
用ADOX创建的,在工程里添加引用dll就不说了,错误的步骤如下(就少了一步):
-------------------------------------------------------------------
//创建库
ADOX.CatalogClass catLog = new ADOX.CatalogClass();
catLog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source ="+path+";");
//创建表
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = catLog;
tbl.Name = "NewTable";
//增加字段
ADOX.ColumnClass c = new ADOX.ColumnClass();
c.ParentCatalog = catLog;
c.Name = list1[i];
c.Properties["Jet OLEDB:Allow Zero Length"].Value = true;
tbl.Columns.Append(c, ADOX.DataTypeEnum.adLongVarWChar, 16);
catLog.Tables.Append(tbl);
---------------------------------------------------------------------
Access的备注类型是Memo,不过在ADOX方式下,DataTypeEnum枚举里是没有这项的,adLongVarWChar就是备注,长度为16。这样创建,是不报错的,但是无论如何,建立出来的都是文本型的字段,最大长度限制是255,在内容很多的时候不够用,查了一堆,发现就是因为少了一句,正确的如下:
-------------------------------------
//创建库
ADOX.CatalogClass catLog = new ADOX.CatalogClass();
catLog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source ="+path+";");
//创建表
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = catLog;
tbl.Name = "NewTable";
//增加字段
ADOX.ColumnClass c = new ADOX.ColumnClass();
c.ParentCatalog = catLog;
c.Type = ADOX.DataTypeEnum.adLongVarWChar; //这句不能少,并且位置必须在其它属性前面,否则会报错。
c.Name = list1[i];
c.Properties["Jet OLEDB:Allow Zero Length"].Value = true;
tbl.Columns.Append(c, ADOX.DataTypeEnum.adLongVarWChar, 16);
catLog.Tables.Append(tbl);
-------------------------------------
这样,就建立出来备注类型的字段了。

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.

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.

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.

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

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.

Nginx performance monitoring and troubleshooting are mainly carried out through the following steps: 1. Use nginx-V to view version information, and enable the stub_status module to monitor the number of active connections, requests and cache hit rate; 2. Use top command to monitor system resource occupation, iostat and vmstat monitor disk I/O and memory usage respectively; 3. Use tcpdump to capture packets to analyze network traffic and troubleshoot network connection problems; 4. Properly configure the number of worker processes to avoid insufficient concurrent processing capabilities or excessive process context switching overhead; 5. Correctly configure Nginx cache to avoid improper cache size settings; 6. By analyzing Nginx logs, such as using awk and grep commands or ELK

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.

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.
