ʹÓÃrubyÓëMS AccessÊý¾Ý¿â½»»¥
ruby³£¹æ·ÃÎÊaccessÊý¾Ý¿âµÄ·½·¨Ó¦¸ÃÊÇʹÓÃDBI¿â
£º
require 'dbi'
DBI.connect("DBI:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;")
¿ÉÊÇ
¼òµ¥³¢ÊÔÖ®ºóûÄܳɹ¦£¬ÌáʾÕÒ²»µ½Çý¶¯Æ÷ADO,ÀÁµÃÔÙÊÔ£¬ËìÕÒÆäËû·½·¨¡£
Ò»·¬ËÑË÷Ö®ºó£¬·¢ÏÖ¿ÉÒÔÓÃWIN32OLEÀ´·ÃÎÊaccess,дһ¸ö¼òµ¥µÄÀà°ü×°Ö®:
require 'win32ole'
class AccessDb
attr_accessor :mdb, :connection, :data, :fields
def initialize(mdb=nil)
@mdb = mdb
@connection = nil
@data = nil
@fields = nil
end
def open
connection_string = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
connection_string << @mdb
@connection = WIN32OLE.new('ADODB.Connection')
@connection.Open(connection_string)
end
def query(sql)
recordset = WIN32OLE.new('ADODB.Recordset')
recordset.Open(sql, @connection)
@fields = []
recordset.Fields.each do |field|
@fields << field.Name
end
begin
@data = recordset.GetRows.transpose
rescue
@data = []
end
recordset.Close
end
def execute(sql)
@connection.Execute(sql)
end
def close
@connection.Close
end
end
ʹÓ÷½·¨ÈçÏÂ:
db=AccessDb.new('f:\db.mdb')
db.open
db.query('select * from foods')
db.fields
db.data
db.execute("insert into foods values (3,'xxx',299,'xo','good!');"
db.close
ÏÂÃæÔÙ¸ø³öʹÓÃrubyѹËõÐÞ¸´accessÊý¾Ý¿âµÄÀý×Ó:
require 'win32ole'
def fixaccess(path,newpath)
jet=WIN32OLE.new('JRO.JetEngine')
File.delete(newpath) if File.exist?(newpath)
sp="Provider=Microsoft.ACE.OLEDB.12.0"
ss=sp+";Data Source="+path
sd=sp+";Data Source="+newpath+";Jet OLEDB:Engine Type=5"
jet.CompactDatabase ss,sd
end
±ÈÈçÒªÇóѹËõdb.mdbÎļþ£¬Ñ¹ËõºóµÄÎļþÃûΪnew_db.mdb²Ù×÷ÈçÏÂ:
fixaccess('f:\db.mdb','f:\new_db.mdb')
·½·¨fixaccessÖеÄspÔÚ°²×°ÁËoffice2007µÄϵͳÉÏ
Ïà¹ØÎĵµ£º
Ç°¼¸ÌìÔÚ×Ô¼ºµÄ±Ê¼Ç±¾ÉÏ°²×°ÁËOFFICE2007¡£½ñÌì×öÁËÒ»¸ö°Ù¶ÈÓÑÇéÁ´½Ó¼ì²éµÄ¹¤¾ß£¬ ÔÚÁ¬½ÓACCESS2007Êý¾Ý¿âµÄʱºò£¬Ê¹ÓõÄÇý¶¯´úÂëΪ£º"provider=microsoft.jet.oledb.4.0;data source="&server.mappath("queryrecord.mdb") ¡£È··¢ÏÖÌáʾ£º
Microsoft JET Database Engine ´íÎó ''80004005''
²»¿Éʶ±ðµÄÊý¾Ý¿â¸ñʽ
µ« ......
Á¬½ÓaccessÊý¾Ý¿â´úÂ룬дÔÚÒ»¸öµ¥¶ÀµÄÀàÀï
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace AppWebDLL
{
public class ConnApp
{
  ......
ÕâÊÇÒ»¸ö¿ªÔ´£¬Ãâ·Ñ£¬ÁéÇÉ£¬¼òÒ×µÄweb×Ô¶¯»¯²âÊÔ×éºÏ¿ò¼Ü£»Í¨³£Çé¿ö£¬¿ªÔ´µÄ¶«Î÷×ÜÊǸøÈËÒÔÃÔÈË£¬¿ª·ÅʽµÄ´¥¸Ð£¬µ«Í¬Ê±£¬Ò²ÈÃÈËÄÑÒÔ¿ìËÙ½Ó½ü£¨ÏñÊǶ«·½ÄÐÈË¿´´ýÎ÷·½ÃÀÅ®£©£»ÒªÁ˽âÒ»¸ö¿ªÔ´¶øÇÒ×éºÏƯÁÁµÄ¿ò¼Ü£¬×ÜÊÇÒª·ÑЩÖÜÕÂ......×ܶøÑÔÖ®£¬Ëü²»»áÏñÊշѵĶ«Î÷£¬ÄãºÜÌÖÑáËü£¬µ«²»¿É·ñÈÏ£¬ÄãµÄ¸¶·ÑÆäʵ»»È¡Á˱ðÈ˵ÄËöËéÀͶ ......
a £½ “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Êý¾Ý¿â.mdb;Password=¹ÜÀíÔ±ÃÜÂë;User ID=¹ÜÀíÔ±ÕʺÅ;Jet OLEDB:Database Password=Êý¾Ý¿âÃÜÂë;Jet OLEDB:System database=d:\system.MDW;” ' ´Ë´¦Óû§ÕÊ»§±ØÐë¾ßÓйÜÀíԱȨÏÞ²ÅÐÐ,´Ë´¦ÊÇÖ¸Á¬½ÓÐèҪѹËõµÄÊý¾Ý¿âµÄÁ¬½Ó´úÂë.
b £½ &ldq ......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Str
{
public partial class Form1 : F ......