To connect to MySQL, Microsoft SQL Server (MSSQL), and MongoDB with Delphi, you can use different components or libraries that provide the necessary database connectivity. Delphi supports a variety of data access components for these databases. Below are examples for each database:
Connecting to MySQL:
For MySQL, you can use dbExpress components provided by Embarcadero. Make sure you have MySQL dbExpress driver installed. You can use the TSQLConnection component to establish a connection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
uses Data.DBXMySql, Data.SqlExpr; var MySQLConnection: TSQLConnection; begin MySQLConnection := TSQLConnection.Create(nil); try MySQLConnection.DriverName := ‘MySQL’; MySQLConnection.LibraryName := ‘dbxmys.dll’; // MySQL dbExpress driver library MySQLConnection.GetDriverFunc := ‘getSQLDriverMYSQL’; MySQLConnection.VendorLib := ‘libmysql.dll’; // MySQL client library MySQLConnection.Params.Add(‘Database=mydatabase’); MySQLConnection.Params.Add(‘User_Name=myuser’); MySQLConnection.Params.Add(‘Password=mypassword’); MySQLConnection.Connected := True; // Use MySQLConnection to interact with the MySQL database finally MySQLConnection.Free; end; end; |
Connecting to Microsoft SQL Server (MSSQL):
For MSSQL, you can use dbExpress components with the MSSQL dbExpress driver. Ensure you have the necessary dbExpress driver installed for MSSQL.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
uses Data.DBXMSSQL, Data.SqlExpr; var MSSQLConnection: TSQLConnection; begin MSSQLConnection := TSQLConnection.Create(nil); try MSSQLConnection.DriverName := ‘MSSQL’; MSSQLConnection.LibraryName := ‘dbxmss.dll’; // MSSQL dbExpress driver library MSSQLConnection.GetDriverFunc := ‘getSQLDriverMSSQL’; MSSQLConnection.Params.Add(‘Database=mydatabase’); MSSQLConnection.Params.Add(‘User_Name=myuser’); MSSQLConnection.Params.Add(‘Password=mypassword’); MSSQLConnection.Connected := True; // Use MSSQLConnection to interact with the MSSQL database finally MSSQLConnection.Free; end; end; |
Connecting to MongoDB:
For MongoDB, you can use the official MongoDB driver for Delphi, which is available on GitHub. You can use the TMongoConnection component to connect to MongoDB.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
uses System.SysUtils, System.Classes, MongoDB.Driver; var MongoDBConnection: TMongoConnection; begin MongoDBConnection := TMongoConnection.Create(nil); try MongoDBConnection.Server := ‘localhost’; // MongoDB server address MongoDBConnection.Port := 27017; // MongoDB server port MongoDBConnection.Username := ‘myuser’; MongoDBConnection.Password := ‘mypassword’; MongoDBConnection.Database := ‘mydatabase’; MongoDBConnection.Open; // Use MongoDBConnection to interact with the MongoDB database finally MongoDBConnection.Free; end; end; |
Make sure to replace placeholders like 'localhost', 27017, 'myuser', 'mypassword', 'mydatabase' with your actual database connection details.
Remember to handle exceptions appropriately in your code and perform error checking during database operations.
Leave a Reply