简体   繁体   中英

Link Delphi to SQL Server

I'm using Delphi XE4 & SQL server express 12, locally on C drive. I can link Delphi to Access DB via ADO comp. Problem is when I try linking SQL server db - it doesn't see my server/db. I'm not sure how I should specify my server name (USER-PC\\SQLEXPRESS - name created by SQLserver) or my db name (C:\\Program Files(x86)\\Microsoft SQL Server\\MSSQL11.SQLExpress\\MSSQL\\DATA\\TestDB.Mdf). No passwords used. I tried via ADoTable (SQL SERVER Native Client 11.0 as well as Microsoft OLE DB Provider for SQL Server) and TSQLConnection. (I've been using MS Access lately - out of touch with external db connection...)

You need to provide the server information somehow, you can use a ConnectionString which is then used to build the connection. The connection string looks like this:

ConnString =
  'Provider=SQLOLEDB.1;Persist Security Info=False;' +
  'User ID=%s;Password=%s;Data Source=%s;Use Procedure for Prepare=1;' +
  'Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;'+
  'Tag with column collation when possible=False';

Take a look at this example uing ADOQuery.

And take a look at the documentation: Connecting to Databases - Overview

If you right-click on the TADOConnection component, you will have an option to Edit Connection String. This opens a dialog, with a button labelled Build...

Clicking this opens a wizard. Select "Microsoft OLE DB Provider for SQL Server" as the provider, and click Next. You are now offered a list of all the SQL Servers that are currently visible. If yours is not in this list then it is unavailable. Has the SQL Server service been started?

If it is in the list then follow the wizard through to build a valid connection string.

Actually, you have to get connected to MSSQL Express with no regard to bitness (32/64). Your SQL Server Express have enabled the TCP/IP and/or Named Pipes connectivity instead of just Shared Memory?

To check, launch SQL Server Configuration Manager (don't know if it changed on 2012, but it's the name on 2008/2008r2) and look under SQL Server Network Configuration/Protocols for < your server instance > item (it'll have 2 of these on 64 bit systems, if my memory not failed me - my machine is 32bit, check on both).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM