将平面MDB转换为SQL链接的mdb后,代码停止工作

时间:2011-05-13 14:22:22

标签: c# ms-access ole

我修改了我的代码来计算mdb文件中每个表中的记录。它运行完美,直到它使用指向SQL服务器的链接表命中MDB文件。无法将代码修改为直接指向SQL服务器,而是用于就地软件升级方案。

如何修改此代码以使用常规表和链接表?

using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Collections;
using System.Data.Common;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] databases = new string[3];
            databases[0] = "data.mdb";
            databases[1] = "chunk.mdb";
            databases[2] = "transactions.mdb";

            DateTime dt = DateTime.Now;
            string filename = "Results-" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + ".txt";
            System.IO.StreamWriter file = new System.IO.StreamWriter(filename);



        foreach(string db in databases)
        {
                    file.WriteLine("##########BEGIN " + db + "##########\r\n");
                    Console.Write("Processing " + db + " Database . . . ");

                    string strAccessConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\financial\" + db;

                    DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
                    DataTable userTables = null;
                    DataSet myDataSet = new DataSet();
                    OleDbConnection conn = new OleDbConnection(strAccessConn);

                    using (DbConnection connection = factory.CreateConnection())
                    {
                        connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\financial\DATA\" + db;
                        string[] restrictions = new string[4];
                        restrictions[3] = "Table"; 

                        try
                        {
                            connection.Open();
                        }
                        catch
                        {
                            Console.WriteLine("Error opening MDB file. Please ensure it is in the correct location");
                        }

                        userTables = connection.GetSchema("Tables", restrictions);
                    }


                   ArrayList tables = new ArrayList();
                   for (int i=0; i < userTables.Rows.Count; i++)
                        tables.Add(userTables.Rows[i][2].ToString());


                   foreach (string tbl in tables)
                   {
                            string queryString = "SELECT COUNT(*) FROM " + "[" + tbl + "]";
                            OleDbCommand command = new OleDbCommand(queryString, conn);
                            command.Connection.Open();

                            try
                            {
                                int records = (int)command.ExecuteScalar();
                                file.WriteLine("{0,-45}" + records,tbl);
                            }
                            catch (OleDbException e)
                            {
                                Console.WriteLine(e + "OLEDB Exception Occured.");
                            }

                            command.Connection.Close();
                   }


                    file.WriteLine("\r\n##########END " + db + "##########");
                    Console.WriteLine("Done!\n");
         }

            file.Close();
            Console.WriteLine(@"All Databases Complete. Press any key to continue...");
            Console.Read();  // Press any key to continue...
            System.Diagnostics.Process.Start("notepad.exe",filename);

        }
    }
}

2 个答案:

答案 0 :(得分:2)

嗨,我相信ado.net无法做到,几年前我和ado.net有同样的情况。你必须使用ADOX。

这是一个有用的ADOX示例列表 http://allenbrowne.com/func-adox.html

ADOX在表和链接表之间没有区别。生病看看我的硬盘,我可能会稍后发布一个例子。

你应该用ADOX执行你的查询,你不会遇到与ADO.net相同的问题

答案 1 :(得分:1)

您也可以使用DAO执行此操作。