VC++ADOX鍒涘缓鏁版嵁搴
鍙戝竷: 2008-7-08 09:03 | 浣滆: webmaster | 鏉ユ簮: 鏈珯鍘熷垱 | 鏌ョ湅: 632娆
浣犲彲浠ヤ娇鐢ㄥ凡缁忓瓨鍦ㄧ殑鏁版嵁搴撴垨鍒欏垱寤轰竴涓綘鑷繁鐨勬暟鎹簱銆侫DO鑷韩涓嶈兘鎻愪緵鍒涘缓鏁版嵁搴撶殑鏂规硶銆備綘鍙互浣跨敤Microsoft ActiveX Data Objects Extensionsf or Data Definition Language and Security,绠绉癆DOX鏉ュ垱寤恒傚湪浣跨敤ADOX鍓嶏紝浣犲繀椤诲弬鑰冧綘鐨勯」鐩紝鍦╒isual C++ Express鐨勪富鑿滃崟涓紝浣犲彲浠ョ偣鍑, Project -> References... 鍙抽敭鐐瑰嚮椤圭洰鍚嶏紝鐐瑰弬鑰

鍦ㄥ睘鎬ч〉锛岀偣鏂板 鍦–OM椤碉紝鎸囧畾 Microsoft ADO Ext. 2.7 or 2.8 for DDL and Security:

After referencing the library, you can use its classes. The classes of the ADOX library are stored in a namespace named ADOX.
The Catalog Class
To create and manage various objects of a database, the ADOX namespace provides an interface named Catalog and its derived class named CatalogClass. To use this class, you can first declare a handle to it. Here is an example:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX;
}
After declaring the class, you can initialize it using the New operator:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX;
catADOX = gcnew ADOX::Catalog;
}
Database Creation
The Connection String of the Catalog
To support database creation, the Catalog interface (or the CatalogClass class is equipped with the Create() method. Its syntax is:
System::Object ^ Create(System::String ^ ConnectionString)
The Create() method takes one argument referred to as the connection string. 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:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX = gcnew ADOX::Catalog;
catADOX->Create(L"Provider=");
}
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:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX = gcnew ADOX::Catalog;
catADOX->Create(L"Provider=Microsoft.JET.OLEDB.4.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:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX = gcnew ADOX::Catalog;
catADOX->Create(L"Provider=Microsoft.JET.OLEDB.4.0");
}
You can also include the name of the provider as its own string. To do that, you can include it in single-quotes:
Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
' Using the Catalog class of the ADOX namespace
Dim catADOX As ADOX.Catalog = New ADOX.Catalog
catADOX.Create("Provider='Microsoft.Jet.OLEDB.4.0'"
End Sub
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 Exercise that would reside in a folder called Exercises of the C: drive, you can specify the connection string as follows:
System::Void btnCreateDatabase_Click(System::Object^ sender, System::EventArgs^ e)
{
ADOX::Catalog ^ catADOX = gcnew ADOX::Catalog;
catADOX->Create(L"Provider='Microsoft.JET.OLEDB.4.0';" +
"Data Source=C:\\Programs\\Exercise1.mdb");
}
You can also include the value of the Data Source key in single-quotes.
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.




发表于 2009-1-5 19:48
| 











