将平面 MDB 转换为 SQL 链接 MDB 后,代码不再计算记录数
我使用了来自 technet 的示例代码来计算 MDB 文件中的记录。将 Access 数据库中的表转换为链接到 SQL(使用 ODBC)后,该程序不再工作。有没有一种简单的方法来修改此代码,以便它仍然可以通过查询 MDB 文件来获取记录计数?
奇怪的是,您可以像平常一样打开 MDB 文件并查看数据,它只是从 SQL 中提取数据。为什么这段代码在查询 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]);
}
}
}
代码在这里找到:http://msdn.microsoft。 com/en-us/library/aa288452(v=vs.71).aspx
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.microsoft.com/en-us/library/aa288452(v=vs.71).aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是更容易吗?
执行操作然后从数据集中的第一个(也是唯一一个)记录中提取
rowCount
Wouldn't it be easier to do a
and then pull
rowCount
from the first (and only) record in the data set.您说它不再起作用并暗示这是计数问题,但是哪一部分不起作用?
Rows.Count 是否为 0,但数据表中的行仍然存在?或者数据集中没有表格或者什么?
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?