473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Create Access Database in C#

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?

Nov 17 '05 #1
9 36264
Hello Brian,

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

CREATE DATABASE [database name]

Martin.
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?


Nov 17 '05 #2
"Martin Carolan" <mc******@gmail.com> wrote in message
news:21*********************@news.cable.ntlworld.c om...
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...?
Nov 17 '05 #3
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.
Nov 17 '05 #4
"Brian Kitt" <Br*******@discussions.microsoft.com> wrote in message
news:41**********************************@microsof t.com...

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
Nov 17 '05 #5
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Brian,


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.templ ate.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;
}
}
Nov 17 '05 #6
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Op**************@TK2MSFTNGP14.phx.gbl...

Oops - forgot the two object variables above the try..catch...finally:

Stream objStream = null;

FileStream objFileStream = null;

try
Nov 17 '05 #7
"Mark Rae" wrote:
if (objStream != null)
{
objStream = null;
}
}


on an unrelated note, lines like this are completely useless.
Nov 17 '05 #8
"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
"Mark Rae" wrote:
if (objStream != null)
{
objStream = null;
}
}


on an unrelated note, lines like this are completely useless.


I see...
Nov 17 '05 #9
Thanks a lot, both of these really helped.

"Mark Rae" wrote:
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Op**************@TK2MSFTNGP14.phx.gbl...

Oops - forgot the two object variables above the try..catch...finally:

Stream objStream = null;

FileStream objFileStream = null;

try

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Albretch | last post by:
.. I am trying to create a database in a MS Access DB via JDBC drivers. I have tried both sun.jdbc.odbc.JdbcOdbcDriver and ids.sql.IDSDriver From some reason both drivers Exceptions tell me...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
0
by: Joaquim Pinto | last post by:
Hi all, I'm creating an access database programatically. I use the ADOX.DataTypeEnum to decide which kind of data will be inserted on a paricular column, date, integer, char, etc. the databse is...
3
by: cql90 | last post by:
Hi all PRO, I don't know how to create the MSAccess database in the runtime with C#, Are there any body know how to do this? I am pretty sure that all of you know how to do it, except me :( ....
2
by: Nitin | last post by:
how can I create Ms acess database or tables thorugh VB.Net program Thanx
1
by: sunlight_sg | last post by:
Hello, i am using ADOX + VB .NET to create a Access Database programmatically. I plan to set some properties of the column such primary key. The code is as follows: Dim cat As ADOX.Catalog...
7
by: Allison | last post by:
Hi -- we are a small manufacturing looking for a multi-user database to take customer orders (nothing too complicated, with 3 users total). We think we should be using Access, but are wondering...
1
by: dave | last post by:
Hello: In Visual Studio 6.0 there is "VisData" to create Access database files. (file.MDB". Q: Does Visual Studio 2005 have tool(s) that create Access database files (file.MDB) ? thank...
1
by: vinudaya | last post by:
Please help me regarding this, I'm new to access, can any one tell me 1. how to create a backup as well as 2.how to connect to server I have already created a database but, i don't know...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.