试工厂模式连数据库,帮看看哪里问题
using System;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
openFileDialog1.Filter = "数据库 (*.mdb)|*.mdb";
openFileDialog1.ShowDialog();
String accessname = openFileDialog1.FileName;
String contype = "System.Data.OleDb";
String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + accessname + ";User ID=Admin;Password=;";
try
{
DbProviderFactory factory = DbProviderFactories.GetFactory(contype);
DbConnection con = factory.CreateConnection();
//到这步连接报错未被捕捉到,连接未正常建立,正确写法该如何改ServerVersion “con.ServerVersion”引发了“System.InvalidOperationException”类型的异常 string {System.InvalidOperationException}
con.ConnectionString = conStr;
DbCommand cmd = factory.CreateCommand();
cmd.Connection = con;
DbDataAdapter dapter = factory.CreateDataAdapter();
}
catch (System.InvalidOperationException e)//ClassNotFoundException e)
{ //捕获驱动类无法找到异常
//throw e; //e.printStackTrace();
System.Diagnostics.Trace.Assert(false, e.Message, "");
System.Environment.Exit(0);
}
}
}
}
------解决方案--------------------
USING OLEDB....
这里是不是都是高手....