Database Creation |
|
Creating a Database |
The Catalog Class |
To support database creation, the ADO library provides an interface named Catalog. In the .NET Framework, this interface is type-defined as the CatalogClass class. To support the creation of a database, the CatalogClass class is equipped with a method named Create. Its syntax is: public object Create(string ConnectionString); The Create() method takes one argument referred to as the connection string. Here is an example of calling it: |
using System; public class Program { static int Main() { ADOX.CatalogClass catDatabase; catDatabase = new ADOX.CatalogClass(); catDatabase.Create(""); return 0; } } This string is made of sections separated by semi-colons. The formula used by these sections is: Key1=Value1;Key2=Value2;Key_n=Value_n;
The first part of the connection string is called the provider. It is software that handles the database. To specify it, assign the desired name to the provider key. Here is an example: using System; public class Program { static int Main() { ADOX.CatalogClass catDatabase; catDatabase = new ADOX.CatalogClass(); catDatabase.Create("Provider="); return 0; } } There are various providers in the database industry. One of them is Microsoft SQL Server and it is represented by SQLOLEDB. If you want to create a Microsoft SQL Server database, specify this provider. Here is an example: using System; public class Program { static int Main() { ADOX.CatalogClass catDatabase; catDatabase = new ADOX.CatalogClass(); catDatabase.Create("Provider=SQLOLEDB;"); return 0; } } When creating this type of database, there are some other pieces of information you must provide in the connection string. Another provider is the Microsoft JET database engine represented as Microsoft.JET.OLEDB.4.0. To create a database for it, specify its provider accordingly. Here is an example: using System; public class Program { static int Main() { ADOX.CatalogClass catDatabase; catDatabase = new ADOX.CatalogClass(); catDatabase.Create("Provider=Microsoft.Jet.OLEDB.4.0;"); return 0; } }
A database is created as a computer file and it has a path, that is, where the database file is located. The path to a file is also known as its location. The path to a database, including its name, is also called the data source. In some of your database operations, you will be asked to provide a data source for your database. In this case, provide the complete path followed by the name of the database. If you are creating a database, the second part of the connection string can be used to specify the path and the name of the database. This section must start with the Data Source key and assigned the path that consists of the drive and the folder(s). After the last folder, the name of the database must have the .mdb extension. For example, to create a database called Exercise1 that would reside in a folder called Programs on the C: drive, you can specify the connection string as follows: using System; public static class Program { static int Main() { ADOX.CatalogClass catADO = new ADOX.CatalogClass(); catADO.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source='C:\\Programs\\Exercise1.mdb'"); Console.WriteLine("A new Microsoft JET database named " + "Exercise1.mdb has been created"); return 0; } } This would produce: A new Microsoft JET database named Exercise1.mdb has been created Press any key to continue . . . Instead of directly passing a string to the Create() method, you can first declare a string variable, initialize it with the necessary provider/data source, and then pass that string variable to the Create() method. Here is an example: using System; public class Program { static int Main() { ADOX.CatalogClass catDatabase; string strDatabase = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source='C:\\Programs\\Exercise1.mdb'"; catDatabase = new ADOX.CatalogClass(); catDatabase.Create(strDatabase); Console.WriteLine("A new Microsoft JET database named " + "Exercise1.mdb has been created\n"); return 0; } }
If you plan to access your database from another programming environment, then you should create an ODBC data source. To do this, in the Control Panel or the Administrative Tools, double-click Data Source (ODBC) to open the ODBC Data Source Administrator: To proceed, click the Add button. This would launch a wizard. In the first page of the Create New Data Source wizard, click Microsoft Access Driver (*.mdb): Click Finish. In the following screen, you would be asked to enter a name for the data source. You can enter the name in one or more words. The name would be used by the applications that need to access the database. This means that you should pay attention to the name you give. In the Description text box, you can enter a short sentence anyway you like. To specify the database that would be used, click Select and select an mdb database. Here is an example: After selecting the necessary database, if you need to be authenticated in order to use the database (if the database is protected), click the Advanced button. By default, a database is meant to allow anybody to use it. In this case, you can leave the Login Name and the Password empty. Otherwise, type the necessary credentials: After using the Set Advanced Options dialog box, click OK (or Cancel to keep it the way it previously was).
After entering the necessary information and selecting the desired database, you can click OK twice.
As mentioned earlier, Microsoft ActiveX Data Objects, or ADO, is a library used to create databases. Before using this library, you must import it in your application. To do this, you can right-click the References node in the Solution Explorer for your project and click Add Reference... In the COM tab of the Add Reference dialog box, select the latest Microsoft ActiveX Data Object Library: And click OK. This would add the ADO reference to your project.
The ADO library is represented in the .NET Framework by the ADODB namespace. It contains various classes that you can access by qualifying them through this namespace. As done with the other classes that belong to a namespace, if you want, in the top section of the file where you would use ADO, you can add the ADODB namespace. Here is an example: using System; using ADODB; public class Program { static int Main() { return 0; } } |
|
||
Previous | Copyright © 2007-2013, FunctionX | Next |
|