How to create an Access database at runtime in C #?

How to create an Access database at runtime in C #?

+3
source share
5 answers

The first thing you need to do is get the COM link on Microsoft ADO Ext. XX for DDL and security. XX represents any version that you have on your computer. My version was 2.7, but with Visual Studio 2008 it was updated to 6.0.

alt text http://blog.jrpsoftware.com/content/binary/WindowsLiveWriter/CreateanAccessDatabaseinC_10DDD/AddReference_2.png

After adding the link, ADOX will be added to the section for using your code.

alt text http://blog.jrpsoftware.com/content/binary/WindowsLiveWriter/CreateanAccessDatabaseinC_10DDD/Using_2.png

. CatalogClass.

CatalogClass cat = new CatalogClass();  
string tmpStr;  
string filename = "Sample.MDB";   
tmpStr = "Provider=Microsoft.Jet.OLEDB.4.0;";   
tmpStr += "Data Source=" + filename + ";Jet OLEDB:Engine Type=5";  
cat.Create(tmpStr);

. , .

 Table nTable = new Table(); 
 nTable.Name = "PersonData"; 
 nTable.Columns.Append("LastName", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("FirstName", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("Address 1", DataTypeEnum.adVarWChar, 45);
 nTable.Columns.Append("Address 2", DataTypeEnum.adVarWChar, 45); 
 nTable.Columns.Append("City", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("State", DataTypeEnum.adVarWChar, 2);
 nTable.Columns.Append("Zip", DataTypeEnum.adVarWChar, 9);
 cat.Tables.Append(nTable);

. , , com .

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(nTable); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);    
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);

. Access, . , .

: John Russell Plant - #

+13

.

, , , , script, .

+5

John Russell Plant , . :

  • .
  • .
  • COM-.
+2
source

Try:

using ADOX; //Requires Microsoft ADO Ext. 2.8 for DDL and Security
using ADODB;

public bool CreateNewAccessDatabase(string fileName)
{
  bool result = false; 

  ADOX.Catalog cat = new ADOX.Catalog();
  ADOX.Table table = new ADOX.Table();

  //Create the table and it fields. 
  table.Name = "Table1";
  table.Columns.Append("Field1");
  table.Columns.Append("Field2");

  try
  {
    cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5");
    cat.Tables.Append(table);

    //Now Close the database
    ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
    if (con != null)
    con.Close();

    result = true; 
  }
  catch (Exception ex)
  {
    result = false;
  }
  cat = null;
  return result;
} 

http://zamirsblog.blogspot.com/2010/11/creating-access-database.html

+2
source
static void IF_EXISTS_DELETE_AND_CREATE(string cn)
{
    //cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =";
    //cn += AppDomain.CurrentDomain.BaseDirectory.ToString() + "test.mdb"; 
    try
    {
        OleDbConnection connection = new OleDbConnection(cn);
        object[] objArrRestrict;
        objArrRestrict = new object[] { null, null, null, "TABLE" };
        connection.Open();
        DataTable schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, objArrRestrict);
        connection.Close();
        string[] list;
        if(schemaTable.Rows.Count > 0)
        {
            list = new string[schemaTable.Rows.Count];
            int i = 0;
            foreach (DataRow row in schemaTable.Rows)
            {
                list[i++] = row["TABLE_NAME"].ToString();
            }
            for ( i = 0; i < list.Length; i++)
            {
                if(list[i] == "TEMP")
                {
                    string deletedl = "DROP TABLE TEMP";
                    using (OleDbConnection conn = new OleDbConnection(cn))
                    {
                        using (OleDbCommand cmd = new OleDbCommand(deletedl, conn))
                        {

                            conn.Open();
                            cmd.ExecuteNonQuery();
                            conn.Close();
                        }
                    }
                    break;
                }
            }
        }
        string ddl = "CREATE TABLE TEMP (USERID INTEGER NOT NULL,[ADATE] TEXT(20), [ATIME] TEXT(20))";
        using(OleDbConnection conn = new OleDbConnection(cn))
        {                    
            using(OleDbCommand cmd = new OleDbCommand(ddl, conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
    catch (System.Exception e)
    {
        string ex = e.Message;
    }
}
0
source

All Articles