开发者

Code no longer works to count records after converting flat MDB to SQL-linked MDB

I have used this example code from technet to count records in an MDB file. After converting the tables in the access database to link to SQL (using ODBC), the program no longer works. Is there an easy way to modify this code so it can still obtain record counts by querying the MDB file?

The odd thing is, you can open the MDB file and view the data as normal, it just pulls it from SQL. Why can't this code do the same when querying the mdb?

using System;
using System.Data;
using System.Data.OleDb;
using System.Xml.Serialization;

public class MainClass {
    public static void Main ()
    {
        string strAccessConn = 
                    "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=BugTypes.MDB";
        string strAccessSelect = "SELECT * FROM Categories";
        DataSet myDataSet = new DataSet();
        OleDbConnection myAccessConn = null;
        try
        {
            myAccessConn = new OleDbConnection(strAccessConn);
        }
        catch(Exception ex)
        {
            Console.WriteLine(
                "Error: Failed to create a database connection. \n{0}", 
                ex.Message);
            return;
        }

        try
        {                
            OleDbCommand myAccessCommand = 
                                new OleDbCommand(strAccessSelect,myAccessConn);
            OleDbDataAdapter myDataAdapter = 
                                new OleDbDataAdapter(myAccessCommand);

            myAccessConn.Open();
            myDataAdapter.Fill(myDataSet,"Categories");

        }
        catch (Exception ex)
        {
            Console.WriteLine(
              "Error: Failed to retrieve the required data from the DataBase.\n{0}",
              ex.Message);
            return;
        }
        finally
        {
            myAccessConn.Close();
        }
        DataTableCollection dta = myDataSet.Tables;
        foreach (DataTable dt in dta)
        {
            Console.WriteLine("Found data table {0}", dt.TableName);
        }
        Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
        Console.WriteLine("{0} tables in data set", dta.Count);
        Console.WriteLine("{0} rows in Categories table",
                            myDataSet.Tables["Categories"].Rows.Count);
        Console.WriteLine("{0} columns in Categories table",
                            myDataSet.Tables["Categories"].Columns.Count);
        DataColumnCollection drc = myDataSet.Tables["Categories"].Columns;
        int i = 0;
        foreach (DataColumn dc in drc)
        {
            Console.WriteLine("Column name[{0}] is {1}, of type {2}", i++,
                                dc.ColumnName, dc.DataType);
        }
        DataRowCollection dra = myDataSet.Tables["Categories"].Rows;
        foreach (DataRow dr in dra)
        {
            Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]);
        }          
    }
}

code found here: http://msdn.开发者_StackOverflow社区microsoft.com/en-us/library/aa288452(v=vs.71).aspx


Wouldn't it be easier to do a

Select count('x') as rowCount from categories

and then pull rowCount from the first (and only) record in the data set.


You say it no longer works and imply it's an issue with counts, but which part isn't working?

Does Rows.Count has 0 but the Rows in the datatables are there? Or are there no tables in the dataset or what?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜