using ADOX;
using ADODB;
using System;
using System.Data;
using System.Data.OleDb;
namespace ADOXJetXML
{
/// <summary>
/// Summary description for DatasetToJet.
/// </summary>
public class DatasetToJet
{
static public void CopyDatasetSchemaToJetDB(DataSet ds, string jetFileName)
{
string connstr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source={0};Jet OLEDB:Engine Type=5;", jetFileName);
Catalog cat = new CatalogClass();
if (!System.IO.File.Exists(jetFileName))
cat.Create(connstr);
else
{
cat.ActiveConnection = new ADODB.ConnectionClass();
(cat.ActiveConnection as ADODB.Connection).Open(connstr, "","", -1);
}
foreach(DataTable table in ds.Tables)
cat.Tables.Append(CopyDataTable(table));
}
static private ADOX.Table CopyDataTable(DataTable table)
{
ADOX.Table adoxTable = new ADOX.TableClass();
adoxTable.Name = table.TableName;
foreach(System.Data.DataColumn col in table.Columns)
adoxTable.Columns.Append(
new ADOX.ColumnClass(),
TranslateDataTypeToADOXDataType(col.DataType),
col.MaxLength);
return adoxTable;
}
static private ADOX.DataTypeEnum TranslateDataTypeToADOXDataType(Type type)
{
string guid = type.GUID.ToString();
return
guid == typeof(bool).GUID.ToString() ? ADOX.DataTypeEnum.adBoolean :
guid == typeof(byte).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedTinyInt:
guid == typeof(char).GUID.ToString() ? ADOX.DataTypeEnum.adChar :
guid == typeof(DateTime).GUID.ToString() ? ADOX.DataTypeEnum.adDate :
guid == typeof(decimal).GUID.ToString() ? ADOX.DataTypeEnum.adDecimal :
guid == typeof(double).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(Int16).GUID.ToString() ? ADOX.DataTypeEnum.adSmallInt :
guid == typeof(Int32).GUID.ToString() ? ADOX.DataTypeEnum.adInteger :
guid == typeof(Int64).GUID.ToString() ? ADOX.DataTypeEnum.adBigInt :
guid == typeof(SByte).GUID.ToString() ? ADOX.DataTypeEnum.adTinyInt :
guid == typeof(Single).GUID.ToString() ? ADOX.DataTypeEnum.adSingle :
guid == typeof(string).GUID.ToString() ? ADOX.DataTypeEnum.adVarChar :
guid == typeof(TimeSpan).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(UInt16).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedSmallInt :
guid == typeof(UInt32).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedInt :
guid == typeof(UInt64).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedBigInt :
ADOX.DataTypeEnum.adBinary;
}
}
}
ADOXJetXML.DatasetToJet.CopyDatasetSchemaToJetDB(productDS1, "foobar.mdb");
using System;
using ADOX;
using ADODB;
using System.Data;
using System.Data.OleDb;
namespace ADOXJetXML
{
/// <summary>
/// Summary description for DatasetToJet.
/// </summary>
public class DatasetToJet
{
static public void CopyDatasetSchemaToJetDB(DataSet ds, string jetFileName)
{
string connstr = string.Format(@"Data Source=""{0}"";Jet OLEDB:Engine Type=5;"+
@"Provider=""Microsoft.Jet.OLEDB.4.0"";User ID=Admin;", jetFileName);
Catalog cat = new CatalogClass();
if (!System.IO.File.Exists(jetFileName))
cat.Create(connstr);
else
{
ADODB.Connection conn = new ConnectionClass();
conn.Open(connstr, "Admin", "",-1);
cat.ActiveConnection = conn;
}
foreach(DataTable table in ds.Tables)
{
try
{
cat.Tables.Delete(table.TableName);
}
catch{}
ADOX.Table adoxTab = CopyDataTable(table, cat);
cat.Tables.Append(adoxTab);
}
}
static private ADOX.Table CopyDataTable(DataTable table, Catalog cat)
{
ADOX.Table adoxTable = new ADOX.TableClass();
adoxTable.Name = table.TableName;
adoxTable.ParentCatalog = cat;
foreach(System.Data.DataColumn col in table.Columns)
{
ADOX.Column adoxCol = new ADOX.ColumnClass();
adoxCol.ParentCatalog = cat;
adoxCol.Name = col.ColumnName;
adoxCol.Type = TranslateDataTypeToADOXDataType(col.DataType);
if (col.MaxLength >=0 )
adoxCol.DefinedSize = col.MaxLength;
adoxTable.Columns.Append(adoxCol, adoxCol.Type, adoxCol.DefinedSize);
}
return adoxTable;
}
static private ADOX.DataTypeEnum TranslateDataTypeToADOXDataType(Type type)
{
string guid = type.GUID.ToString();
ADOX.DataTypeEnum adoxType =
guid == typeof(bool).GUID.ToString() ? ADOX.DataTypeEnum.adBoolean :
guid == typeof(byte).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedTinyInt:
guid == typeof(char).GUID.ToString() ? ADOX.DataTypeEnum.adChar :
guid == typeof(DateTime).GUID.ToString() ? ADOX.DataTypeEnum.adDate :
guid == typeof(decimal).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(double).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(Int16).GUID.ToString() ? ADOX.DataTypeEnum.adSmallInt :
guid == typeof(Int32).GUID.ToString() ? ADOX.DataTypeEnum.adInteger :
guid == typeof(Int64).GUID.ToString() ? ADOX.DataTypeEnum.adBigInt :
guid == typeof(SByte).GUID.ToString() ? ADOX.DataTypeEnum.adTinyInt :
guid == typeof(Single).GUID.ToString() ? ADOX.DataTypeEnum.adSingle :
guid == typeof(string).GUID.ToString() ? ADOX.DataTypeEnum.adLongVarWChar :
guid == typeof(TimeSpan).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(UInt16).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedSmallInt :
guid == typeof(UInt32).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedInt :
guid == typeof(UInt64).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedBigInt :
ADOX.DataTypeEnum.adBinary;
return adoxType;
}
}
}
using System;
using ADOX;
using ADODB;
using System.Data;
using System.Data.OleDb;
namespace ADOXJetXML
{
public class DatasetToJet
{
static public void CopyDatasetSchemaToJetDB(DataSet ds, string jetFileName)
{
string connstr = string.Format(@"Data Source=""{0}"";Jet OLEDB:Engine Type=5;"+
@"Provider=""Microsoft.Jet.OLEDB.4.0"";User ID=Admin;", jetFileName);
Catalog cat = new CatalogClass();
if (!System.IO.File.Exists(jetFileName))
cat.Create(connstr);
else
{
ADODB.Connection conn = new ConnectionClass();
conn.Open(connstr, "Admin", "",-1);
cat.ActiveConnection = conn;
}
foreach(DataTable table in ds.Tables)
{
try
{
cat.Tables.Delete(table.TableName);
}
catch{}
ADOX.Table adoxTab = CopyDataTable(table, cat);
cat.Tables.Append(adoxTab);
//
// NEW METHOD CALL
//
MoveData(adoxTab, table);
}
}
static private ADOX.Table CopyDataTable(DataTable table, Catalog cat)
{
ADOX.Table adoxTable = new ADOX.TableClass();
adoxTable.Name = table.TableName;
adoxTable.ParentCatalog = cat;
foreach(System.Data.DataColumn col in table.Columns)
{
ADOX.Column adoxCol = new ADOX.ColumnClass();
adoxCol.ParentCatalog = cat;
adoxCol.Name = col.ColumnName;
adoxCol.Type = TranslateDataTypeToADOXDataType(col.DataType);
adoxCol.Attributes = ADOX.ColumnAttributesEnum.adColNullable;
if (col.MaxLength >=0 )
adoxCol.DefinedSize = col.MaxLength;
adoxTable.Columns.Append(adoxCol, adoxCol.Type, adoxCol.DefinedSize);
}
return adoxTable;
}
static private ADOX.DataTypeEnum TranslateDataTypeToADOXDataType(Type type)
{
string guid = type.GUID.ToString();
ADOX.DataTypeEnum adoxType =
guid == typeof(bool).GUID.ToString() ? ADOX.DataTypeEnum.adBoolean :
guid == typeof(byte).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedTinyInt:
guid == typeof(char).GUID.ToString() ? ADOX.DataTypeEnum.adChar :
guid == typeof(DateTime).GUID.ToString() ? ADOX.DataTypeEnum.adDate :
guid == typeof(decimal).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(double).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(Int16).GUID.ToString() ? ADOX.DataTypeEnum.adSmallInt :
guid == typeof(Int32).GUID.ToString() ? ADOX.DataTypeEnum.adInteger :
guid == typeof(Int64).GUID.ToString() ? ADOX.DataTypeEnum.adBigInt :
guid == typeof(SByte).GUID.ToString() ? ADOX.DataTypeEnum.adTinyInt :
guid == typeof(Single).GUID.ToString() ? ADOX.DataTypeEnum.adSingle :
guid == typeof(string).GUID.ToString() ? ADOX.DataTypeEnum.adLongVarWChar :
guid == typeof(TimeSpan).GUID.ToString() ? ADOX.DataTypeEnum.adDouble :
guid == typeof(UInt16).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedSmallInt :
guid == typeof(UInt32).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedInt :
guid == typeof(UInt64).GUID.ToString() ? ADOX.DataTypeEnum.adUnsignedBigInt :
ADOX.DataTypeEnum.adVarBinary;
return adoxType;
}
//
// NEW METHOD
//
private static ADODB.Command ADOXTableInsertCommand(ADOX.Table adoxTab)
{
ADODB.Command result = new ADODB.CommandClass();
object conn = adoxTab.ParentCatalog.ActiveConnection;
result.ActiveConnection = (ConnectionClass) conn;
result.CommandText = string.Format("INSERT INTO {0} ({1}) values({2}) " , adoxTab.Name, "{0}", "{1}");
string colNames = string.Empty;
string colVals = string.Empty;
for (int i = 0; i < adoxTab.Columns.Count; i++)
{
ADOX.Column adoxCol = adoxTab.Columns[i];
string name = adoxCol.Name;
ADOX.DataTypeEnum type = adoxCol.Type;
switch ( type )
{
case ADOX.DataTypeEnum.adVarBinary: break;
default:
colNames += ( colNames != string.Empty ? "," : "") + name;
colVals += (colVals != string.Empty ? "," : "") + "?" ;
break;
}
}
result.CommandText = string.Format(result.CommandText, colNames, colVals);
return result;
}
//
// NEW METHOD
//
private static void MoveData(ADOX.Table adoxTab, DataTable aTable)
{
object i;
ADODB.Command cmd = ADOXTableInsertCommand(adoxTab);
foreach(DataRow row in aTable.Rows)
{
object arry = row.ItemArray;
cmd.Execute(out i, ref arry, 1);
}
}
}
}
Hi there: I am doing this and I get the following message:
System.Runtime.InteropServices.COMException -> DataTypes doesnt match in criteria expresion or something like that, I have my visual studio in spanish.
What could be the problem. It is creating a file and if I open it it have the table and the structure but no records.
Thanks a lot.