python 和 c# 连接数据库 (Access)

时间:2024-11-23 00:07:50

模块pypyodbc 1.3.3下载:   https://pypi.python.org/pypi/pypyodbc/

安装:解压文件找到pypyodbc.py复制到python安装目录Lib文件夹下即可

import pypyodbc 

pypyodbc.win_create_mdb('D:\\database.mdb')

connection_string = 'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\database.mdb'

connection = pypyodbc.connect(connection_string)

SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));'

connection.cursor().execute(SQL).commit()

http://www.next-second.com/s/pypyodbc_tutorial_en.htm

c# 连接数据库 (Access)

1、数据库准备

python 和 c# 连接数据库 (Access)

2、新建一个控制台应用程序

python 和 c# 连接数据库 (Access)

3、直接运行可以使用

python 和 c# 连接数据库 (Access)

4、可以使用一个帮助类来调用

使用注释的部分即可。

5、所有的代码

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb; namespace connAccess
{
class Program
{
static void Main(string[] args)
{
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:/ICCC13.mdb'";
string sqlString = "select * from ChinaNANO2007 where id=207";
OleDbConnection conn = new OleDbConnection(connstring);
OleDbCommand cmd = new OleDbCommand(sqlString, conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
Console.WriteLine(dt.Rows[][].ToString());
Console.WriteLine(dt.Rows[]["surname"].ToString());
cmd.Dispose();
conn.Close();
conn.Dispose();
// DataTable dt = new DataTable();
// dt = ConnHelper.ExecuteDataTable("select * from ChinaNANO2007 where id=207",ConnHelper.connstring);
// Console.WriteLine(dt.Rows[0]["surname"].ToString());
}
}
}
//ConnHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb; namespace connAccess
{
public abstract class ConnHelper
{
public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:/ICCC13.mdb'";
public static DataTable ExecuteDataTable(string SqlString, string connstring)
{
using (OleDbConnection conn = new OleDbConnection(connstring))
{
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand(SqlString, conn);
try
{
conn.Open();
dt.Load(cmd.ExecuteReader());
return dt;
}
catch (System.Exception ex)
{
return null;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
} }
}