Create Access Database in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am very familiar with DataAdapters and accessing Access from C#, but am
trying to create a blank database programatically from C#. I can't figure
out how to do this, and haven't had much luck with MSDN. Anyone here know
how to do this?
 
Hello Brian,

If it's a SQL Server database you can create a blank one my running the query:

CREATE DATABASE [database name]

Martin.
 
If it's a SQL Server database you can create a blank one my running the
query:

CREATE DATABASE [database name]

Did you actually read the OP, particularly the subject...?
 
Actuall I'm using MS Office Access. I don't know of any way to perform SQL
commands BEFORE i attach to a database. I need to create a blank database,
then I know SQL well enough to do what I need to. I just don't know how to
create a blank database programatically.
 
Brian,
Actuall I'm using MS Office Access. I don't know of any way to perform
SQL
commands BEFORE i attach to a database. I need to create a blank
database,
then I know SQL well enough to do what I need to. I just don't know how
to
create a blank database programatically.

It's a breeze, so long as you don't mind a bit of InterOp... :-) Just set a
reference to Microsoft ADO Ext 2.x in your project and drop the following
code into a new Class.

using System;
using ADOX;

namespace CreateDB
{
public class CCreateDB
{
public static bool CreateDB(string pstrDB)
{
try
{
Catalog cat = new Catalog();
string strCreateDB = "";

strCreateDB += "Provider=Microsoft.Jet.OLEDB.4.0;";
strCreateDB += "Data Source=" + pstrDB + ";";
strCreateDB += "Jet OLEDB:Engine Type=5";
cat.Create(strCreateDB);
return true;
}
catch (Exception)
{
throw;
}
}
}
}

Then, to create the database, just run:

bool blnSuccess = CCreateDB.CreateDB(@"c:\test.mdb");

Obviously, you'll want to enhance the above code to e.g. check that the DB
doesn't already exist, check that the path does already exist etc.

HTH

Mark
 

Actually, on reflection, a much more elegant solution might be to include a
blank Access database as an Embedded Resource in your app, as it will be a
mere 64k in length. Then, when you need to create a new DB, just write it
out to the file system wherever you need it, naming it as required.

1) Using Access, create a blank database called something like template.mdb
(or whatever).

2) Create a new C# WinForms project called Brian (or whatever).

3) Drag the newly created DB into your C# project, setting its Build Action
property to Embedded Resource.

4) Use the following code to extract the blank DB out of your assembly and
onto the hard drive:

try
{
byte[] abytResource;
System.Reflection.Assembly objAssembly =
System.Reflection.Assembly.GetExecutingAssembly();

objStream = objAssembly.GetManifestResourceStream("Brian.template.mdb");
abytResource = new Byte[objStream.Length];
objStream.Read(abytResource, 0, (int)objStream.Length);
objFileStream = new FileStream(<path> + "\\" + <newName.mdb>,
FileMode.Create);
objFileStream.Write(abytResource, 0, (int)objStream.Length);
objFileStream.Close();

}
catch (Exception)
{
throw;
}
finally
{
if (objFileStream != null)
{
objFileStream.Close();
objFileStream = null;
}
if (objStream != null)
{
objStream = null;
}
}
 
Oops - forgot the two object variables above the try..catch...finally:

Stream objStream = null;

FileStream objFileStream = null;

try
 
Back
Top