Leere Access Datenbank (mdb) erzeugen?

Ninette

Grünschnabel
Hallo,

ich möchte an einem beliebigen Ort eine leere Access Datenbank erstellen lassen, je nachdem wo der Benutzer sie hinhaben möchte. Wie macht man das am Besten. Muss ich dafür zwingend einen Verweis zu einer DAO COM Komponente machen oder gibt es noch einen anderen Weg?
 
Hab ich in einem Buch gefunden - hoffe es hilft!

using System;
using System.Data;
using System.Data.SqlClient;

namespace Datenbanken_erzeugen
{
class Start
{
[STAThread]
static void Main(string[] args)
{
// Verbindung zum lokalen SQL Server aufbauen
SqlConnection connection = null;
try
{
connection =
new SqlConnection("Server=(local);Trusted_Connection=Yes");
connection.Open();

// Datenbank erzeugen
string databaseName = "Bookstore";
string sql = "CREATE Database " + databaseName;
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();

// Verbindung zur neuen Datenbank aufbauen
connection.Close();
connection =
new SqlConnection("Server=(local);Database=" + databaseName + ";" +
"Trusted_Connection=Yes");
connection.Open();

// Tabellen erzeugen
string sql1 = "CREATE TABLE Authors (" +
"Id int NOT NULL PRIMARY KEY IDENTITY," +
"FirstName nvarchar(255) NOT NULL," +
"LastName nvarchar(255) NOT NULL)";

string sql2 = "CREATE TABLE Books (" +
"Id int NOT NULL PRIMARY KEY IDENTITY," +
"Title nvarchar(255) NOT NULL," +
"ISBN nvarchar(255) NOT NULL," +
"PublishingDate datetime, " +
"Price money NOT NULL DEFAULT 0)";

string sql3 = "CREATE TABLE BookAuthors (" +
"Id int NOT NULL PRIMARY KEY," +
"BookId int NOT NULL," +
"AuthorId int NOT NULL)";

// Die Beziehungen zwischen den Tabellen definieren
string sql4 = "ALTER TABLE BookAuthors " +
"ADD CONSTRAINT FK_Books FOREIGN KEY " +
"(BookId) REFERENCES Books(Id)";

string sql5 = "ALTER TABLE BookAuthors " +
"ADD CONSTRAINT FK_Authors FOREIGN KEY " +
"(AuthorId) REFERENCES Authors(Id)";

// Den endgültigen SQL-String zusammensetzen und ausführen
sql = sql1 + "\r\n" + sql2 + "\r\n" + sql3 + "\r\n" +
sql4 + "\r\n" + sql5;
command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
try
{
connection.Close();
}
catch {}
}

Console.WriteLine("Beenden mit Return");
Console.ReadLine();
}
}
}


lg
Thorsten
 
Das ist leider nur für eine SQL-Datenbank. Was ich suche ist das für eine MDB. Denn ich bin gerade an einem Projekt, wo ich ne SQL Datenbank in eine Access Datenbank sichern möchte. Trotzdem danke.
 
Hab da was, allerdings nur auf Englisch, sollte aber kein Problem darstellen:

1. Open a new Visual C# .NET console application.

2. In the Solution Explorer window, right-click the References node and select
Add Reference.

3. In the Add Reference dialog box, click the COM tab and select Microsoft ADO
Ext. 2.7 for DDL and Security. Click Select to add it to the "Selected
Components". Click OK.

4. Delete all of the code from the code window for Class1.cs.

5. Copy the following code and paste it into the code window:

Code:
using System;
using ADOX;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            ADOX.CatalogClass cat = new ADOX.CatalogClass();

            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
                 "Data Source=D:\\AccessDB\\NewMDB.mdb;" +
                 "Jet OLEDB:Engine Type=5");

            Console.WriteLine("Database Created Successfully");

            cat = null;

        }
    }
}

6. Change the path to the new .mdb file as appropriate. Press F5 to build and
run the project.

The new .mdb file will be created in Access 2000 (Jet 4.0) format.
 

Neue Beiträge

Zurück