?
Solved

Connection toa remote SQL server

Posted on 2000-01-04
17
Medium Priority
?
426 Views
Last Modified: 2010-04-10
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??
0
Comment
Question by:whluk
  • 13
  • 3
17 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2323283
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
0
 

Expert Comment

by:SDU
ID: 2323844
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
0
 

Author Comment

by:whluk
ID: 2325260
what is the typical connection string
under SQL server configuration utility?


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

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

Author Comment

by:whluk
ID: 2325276
And I need it urgently cos' my job needs me to do that.
Thanks
Jacky
0
 

Author Comment

by:whluk
ID: 2325289
Btw, Could you also please show me what I'm supposed to do after connection to the database after OpenEx?
0
 

Author Comment

by:whluk
ID: 2325291
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!
0
 

Expert Comment

by:SDU
ID: 2326755
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.
0
 

Author Comment

by:whluk
ID: 2328475
So did my computer collapse... so may be coming back to you in a few days or two...sorry for any inconvenience caused...
0
 

Author Comment

by:whluk
ID: 2331095
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
0
 

Author Comment

by:whluk
ID: 2331097
One of my collegues supply me a new user name and password. for example name = hello,
password = hello etc
0
 

Accepted Solution

by:
SDU earned 200 total points
ID: 2332196
When you are in the dlg box having 2 radio buttons, you select the one saying

How should SQL Server verify the authenticity of the login ID ?
-> With SQL Server authentication using a login ID and password entered by the user.

Then at the bottom, you enter your login ID and password.

Once your ODBC driver has been all set,
you can verify your data with MSAccess if you want to by using
this ODBC driver you've just created.

To do this, you create a new database in Access;
then, (I have a french MSAccess here, so I don't have the exact names)
in the file menu, you have a choice that should look like "external data"
with a sub menu called "Link table". Select that.
In the dialog that appears, you go in the "file type combo box" and you
select "ODBC database". In the machine data source tab,
you select the driver you've created.
At that point, I'll go by memory since I don't have any SQL Server database on my hand.
Maybe you'll have a security dialog that will ask for your "hello" pwd and uid.
You're supposed to see next a list of table from wich you can select all or only the tables
you want to see.
What all that MsAccess stuff's all about? it can help you visualize or modify the datas in your
SQL Server DB since you're linked with that DB in MSAccess. Those data's are coming directly from
that DB. Be carefull not to modify data you don't want to.
If you save that MSAccess DB, you will not have to go through all those steps again.

Now, for the C++ side, like I said earlier, you can use the Open method.
With that, when you will run, you will see a dialog that will be exactly the same as
the one having the "machine data source" tab you saw in the MSAccess path.
You will be able to select your ODBC source before continuing to run.

If you choose the OpenEx method instead, the connection string will be something like that:
Suppose you named your ODBC driver "MyDriver",
YourDBMember.OpenEx("DSN=MyDriver;UID=Hello;PWD=Hello", CDatabase::noOdbcDialog | CDatabase::useCursorLib);

I hope all these will be enough to answer your question.
I see that we don't live on the same hours, I probably answer when you are sleeping and vice-versa.
I will not be here for a couple of weeks. I'm here till 17h00 today (I'm in Quebec, Canada, so Estern time)
If you have more questions, I'll be back on january 25th.
Good luck !

PS if you have a database already created on SQL Server (and I assume that you do since your collegue gave you a user id and pwd, you don't have to do anything on that side)
0
 

Author Comment

by:whluk
ID: 2333944
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
0
 

Author Comment

by:whluk
ID: 2333992
what is dsn in openex and where should I put the CDatabase and the related stuffs in. Onpaint...? Onclick?
0
 

Author Comment

by:whluk
ID: 2333997
ls there any way that i can view the struture or content of the database in the SQL server
0
 

Author Comment

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

Author Comment

by:whluk
ID: 2334346
or books? journals? or anything
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question