I am reading a database (Ms Access) in C#. I am using Ado.net for connection. This database has lot of tables (around 100) and each and every table has around 50 columns and 500 rows. Each and every field contain different data type (integer, string, bool). What I have to do is I have to convert selected tables and fields to binary format.
After some literature survey I have planned to read the data by using 'DataReader' since I need to read the variables only for the binary conversion.
My question is
In C# programming side what should be my data structure? Say If I create separate classes for all tables and define the member variables and methods, How I can make it more effective?. Because I have mentioned there are 100 tables and 50 fields, Actually I don't need to select all the fields (I have to select the required ones only). I don't want to hard code it like (For example)
SELECT BusinessEntityID, LoginID, NationalIDNumber from table1Name
Because I have to iterate through selected tables and selected fields which is mentioned somewhere in my code. Since I am bit new to SQL, Could you please provide some hints? In another way, If I ask the question how to make the select query efficient with variables for tables and fields (Please correct me if anything wrong with this question)
Update
Something like this mentioned below SQL Server SELECT INTO @variable?
Having the tremendous overall amount of fields as you described (100 x 50 = 5000), it might be useful to read SchemaTable
first using OleDb
:
Listing 1. Get SchemaTable (optional)
static DataTable GetSchemaTable(string connectionString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
DataTable schemaTable =
connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, null, "TABLE" });
return schemaTable;
}
}
Listing 2. Read data from MS Access Database table
The actual procedure to read data from MS Access database table and populate DataTable
in DataSet
using DataAdapter
OleDb
object is shown below (it's been originally encapsulated in my custom DB-operation class, as reflected in syntax, so you can modify it for your needs):
#region DataSet, DataAdapter, DataTable
internal DataSet dataSet;
internal OleDbDataAdapter dataAdapter;
internal DataTable dataTable;
private OleDbConnection connection;
#endregion
internal GetData(string SelectQuery, string ConnectionString)
{
try
{
#region Create Data Objects: Connection, DataAdapter, DataSet, DataTable
// use OleDb Connection to MS Access DB
connection = new OleDbConnection(ConnectionString);
connection.Open();
// create new DataAdapter on OleDb Connection and Select Query text
dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = new OleDbCommand(SelectQuery, connection);
// create DataSet
dataSet = new DataSet();
// retrieve TableSchema
// DataTable[] _dataTablesSchema = _dataAdapter.FillSchema(_dataSet, SchemaType.Source, "{TABLE NAME}");
DataTable[] _dataTablesSchema = dataAdapter.FillSchema(dataSet, SchemaType.Source);
// there is only one Table in DataSet, so use 0-index
dataTable = _dataTablesSchema[0];
// use DataAdapter to Fill Dataset
dataAdapter.Fill(dataTable);
// OPTIONAL: use OleDbCommandBuilder to build a complete set of CRUD commands
OleDbCommandBuilder builder = new OleDbCommandBuilder(dataAdapter);
// Update, Insert and Delete Commands
dataAdapter.UpdateCommand = builder.GetUpdateCommand();
dataAdapter.InsertCommand = builder.GetInsertCommand();
dataAdapter.DeleteCommand = builder.GetDeleteCommand();
#endregion
connection.Close();
}
catch {throw; }
}
See the post on MSDN for more details: link http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.getoledbschematable.aspx
Note : The first step (Listing 1) is optional. Both procedures are based on OleDb
objects to operate on MS Access DB. For other DB types (eg MS SQL server) there are different set of objects, (like SQLConnection
, etc.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.