Link to home
Start Free TrialLog in
Avatar of whluk
whluk

asked on

Connection toa remote SQL server

Hi all,
   I got a problem on connecting SQL server from Visual C++ 6.0. I first open the project as a database project
Then I used 'add data connection' by right clicking on the project name...
then selected open source dialog box comes out, I selected file data source tab afterwards.I chose new and sql server afterwards. 'Create new data source' dialog box came out, I entered a new name for the source afterwards.
'Create a new data source to SQL server'
dialog box came out afterwards, In the 'Which SQL server do you want to connect to?' question, I entered the host number afterwards... I used next button without changes to 'How should SQL server verify the authenticity of login ID' and client configuration button.I pushed next button afterwards..
But the machine returned an error quoted
'Client unable to establish connection'
and 'unable to load communication module and driver has not correctly installed' resulted with ODBC SQL server dirver in brackets... Could anyone show me how to connect to the SQL server correctly... or show me some links about it??
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

On the "How should SQL Server verify..." page try pressing the Client Configuration button. Make sure that the network library is set to one of types supported by your server (and your client). I'd try Named Pipes or TCP/IP if you are not sure. Here is what the help say's about this button:

Client Configuration button

Starts the Add New Network Library Configuration dialog box of the SQL Server Client Configuration.
If you specified a new name in the Server box on the first screen of the wizard, you may need to use this dialog box to add a server alias configuration entry whose name matches the name you specified in the Server box on the first screen of the ODBC wizard. The alias name must match the name you specified in the Server box. For Microsoft SQL Server, you do not usually need a server alias configuration. In most cases, the SQL Server ODBC driver can connect by using the default Net-Library and the network name supplied in the Server box on the first screen of the wizard.

"Click Client Configuration if you want the connection to use a network library other than the client’s default network library. Also click Client Configuration if the actual network address of the server must be specified for a successful connection. For example, when using the TCP/IP Net-Library you may need to specify the port and socket address of the server, or, if a SQL Server is listening on an alternate named pipe, you must specify the pipe name in the advanced entry.

For more information about configuring clients, see SQL Server Books Online."

Good Luck,
Steve
Avatar of SDU
SDU

Hi,

I don't know if this gonna help:

I never used a "database project" and I don't really know what it is,
but here is how to make a quick connection to SQL Server.
You can modify it afterward to better suit your needs.

First, in the control panel, select "ODBC data sources", then under
the "system DSN" tab, click add. Select your SQL Server Driver from the
list. Give a name to your data source. Select also the server
name on which is located your database.
Then we used to put a user name and his password by selecting the second
radio button choice.
You normally don't have to change anything in the other panels except
if you have more than one database on your server (and if the database
you want to connect to is not the default one)

After, in your C++ project (InitInstance of your CWinApp is a good choice)
you can connect to the database using a CDatabase member,
by using the Open method or by using OpenEx and giving the connect string like this:

CDatabase m_MyDB;

m_MyDB.Open(NULL);
or
m_MyDB.OpenEx("DSN=<your DSN>;UID=<your User id>;PWD=<The pwd>",
 CDatabase::noOdbcDialog | CDatabase::useCursorLib);

that should do the job quickly

Hope it helped
Avatar of whluk

ASKER

what is the typical connection string
under SQL server configuration utility?


Avatar of whluk

ASKER

I sort of need some step by step (perhaps in graphical format) help!
Could you please show me some links for this?
Avatar of whluk

ASKER

And I need it urgently cos' my job needs me to do that.
Thanks
Jacky
Avatar of whluk

ASKER

Btw, Could you also please show me what I'm supposed to do after connection to the database after OpenEx?
Avatar of whluk

ASKER

To get some data out of it...
I'm just in a big hurry and every is not in proper places. So please bear with me!
Sorry for the delay, I'm presently having problems with the network here
and my internet connection suffers.

I'll give you an example.
First, set your ODBC driver like I described earlier.

Second, I'll give you an example of writing in a table. This example is using ODBC.

1. Create a new project (File new, project tab, choose MFC app wizard)
   I won't go in details here, I suppose you know the procedure. If not, ask.
   But if you don't, you will need more help on how to use Visual studio
2. Once you project is created, you create a new class derived from CRecordset,
   you will be asked to choose the ODBC source on a second dialog, it's where you
   choose the ODBC source you are supposed to have done on the first place.

3. Here is an example of how to use the recordset

{
   CDatabase DB;
   DB.OpenEx(NULL, CDatabase::useCursorLib);

   TRACE("\n**************************************************\n");
   //
   // Insertion of many rows without using optimization
   //
   {
      rs_toto  RS;      // Here is the name of the class derived from CRecordset

      RS.m_strSort = "";
      RS.m_strFilter = "ID = 123";      // You don't really need this but this speed up the
                                                      // opening of your recordset. ID is the name of my foreign key
      RS.m_pDatabase  =  &DB;
      if ( RS.Open(CRecordset::snapshot,NULL, CRecordset::none) )
      {
         for ( int i=0; i<5000; i++ )
         {
            RS.AddNew();

            RS.m_ID = 123;
            RS.m_VALUE = float(1000 + i);

            if ( !RS.Update() )
            {
               AfxMessageBox("Update Error");
               break;
            }
         }

         RS.Close();
      }
      else
      {
         AfxMessageBox("Open Error");
      }
   }
   TRACE("\n**************************************************\n");
}

In this example, I don't have any connection string, I'll be asked to select my ODBC source at the beginning with a dialog.
Avatar of whluk

ASKER

So did my computer collapse... so may be coming back to you in a few days or two...sorry for any inconvenience caused...
Avatar of whluk

ASKER

Here comes another question
,for setting in the user name and password with the second radio choice,
Do I have to set something on the SQL Server side. like creating device, segments, database or table or the like
...
THanks
JAcky
Avatar of whluk

ASKER

One of my collegues supply me a new user name and password. for example name = hello,
password = hello etc
ASKER CERTIFIED SOLUTION
Avatar of SDU
SDU

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of whluk

ASKER

what about 'which SQL server do you want to connect to?' in the 'Create a new data source to a SQL server dialog'
is it supposed to be a host number like 199.199.199.99 etc or there should be some formatted strings or the host name instead.

Thanks
Jacky
Avatar of whluk

ASKER

what is dsn in openex and where should I put the CDatabase and the related stuffs in. Onpaint...? Onclick?
Avatar of whluk

ASKER

ls there any way that i can view the struture or content of the database in the SQL server
Avatar of whluk

ASKER

Could anybody please supply me some links for complete visual c++ database programming (using MFC, ODBC, OLE DB or/and ADO)?
Avatar of whluk

ASKER

or books? journals? or anything