want to avoid opening odbcdialogbox

Hi!
I have written this code
            CDatabase db;
            CRecordset rec;
            int reccount;
            

            if(db.Open(_T("abc"),FALSE,FALSE,_T("ODBC;"),TRUE)!=0)
                  AfxMessageBox("connected",NULL,MB_OK);
            else
            AfxMessageBox("not connected",NULL,MB_OK);

            
//rec.m_pDatabase = &db;
rec.Open(CDatabase::dyna,_T( "Select * from xyz"));
            
            while(!rec.IsEOF())
            {
                  rec.MoveNext();
            }
            
            reccount=rec.GetRecordCount();
            printf("count = %d",reccount);

I am using SQL-Server7.0 on Win-NT.
Now in this code why is it opening an ODBCdialogBox for selecting the DSN name. If I wanna get rid of that dialogbox and want to hardcore my DSN name how do I do it?
After executing it should directly print the number of records.
There should not be any user interaction.
Thanks.
LVL 2
highmarksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

highmarksAuthor Commented:
oh ya please do not give me answers like use option noodbcdialogbox since I have used them still it is opening the dialog box.
0
krispCommented:
Hi,
 With MS-ACCESS I am not getting this problem. So it might be something related to SQL Server

cheers.
0
inprasCommented:
Hi
use SQLCOnfigureDataSource to configure data source prgrammatically
or cofnfigure the dta source in ODBC as abc U won't get the Dialog box
and also check UR not giving any password and login
When U specify a DSN name which is not configured then ODBC dialog box pops up

Hope this helps
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

wildyCommented:
try to replace the _T("ODBC;") parameter with _T("ODBC;UID=sa")

Note: if sa have a password you must also specify the password.
0
highmarksAuthor Commented:
inprass can u give some code which can be used
0
highmarksAuthor Commented:
inprass can u give some code which can be used
0
inprasCommented:
For Configuring
SQLConfigDataSource(NULL,ODBC_ADD_DSN, "Excel Files (*.xls)",
                   "DSN=New Excel Data Source\0"
                   "Description=New Excel Data Source\0"
                   "FileType=Excel\0"
                   "DataDirectory=C:\\EXCELDIR\0"
                   "MaxScanRows=20\0");

For opening
m_dbCust.Open( _T( "MYDATASOURCE" ), FALSE,
               FALSE, _T( "ODBC;UID=JOES;PWD=MyPSWD" ),TRUE);

U can use same for SQL-server only supply proper driver
Hope this helps
0
SteveGTRCommented:
Call OpenEx() like so:

db.OpenEx("DSN=MYDATASOURCE;UID=sa",
 CDatabase::noOdbcDialog);

You may also or in openExclusive, openReadOnly, useCursorLib.

Good Luck,
Steve
0
highmarksAuthor Commented:
HI! Steve I had written it in my question that I have tried option of noodbcdialog but didn't help.
0
highmarksAuthor Commented:
guys I have tried options like SQLConfigDataSource and SQLDriverConnect. But it still asks me to select DSN name by opening ODBCDialogBox.
0
SteveGTRCommented:
So you've tried OpenEx with the noOdbcOption and it still opened the dialog box? If you haven't I'd give it a try...

Good Luck,
Steve
0
bertpCommented:
Here is what worked for me

1) I did not explicitly open the database; I passed NULL to the CRecordset constructor, and the constructor constructed a CDatabase object for me;

2) I derived a class from CRecordset
  and overrode the GetDefaultConnect()
like this

CString CMySet::GetDefaultConnect()
{
return _T("ODBC;DSN=MYDSNENTRY");
}

(It has been awhile, but if I recall correctly, ClassWizard makes it easy to setup a CRecordset derived class specific to your own Database)


3)and obvisiouly, this assumes that you have previously set a MYDSNENTRY in the ODBC applet to point to a specific database...  Thats it... good luck!  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bertpCommented:
Hi
  If you're still having trouble you should consider rejecting my answer so that you can again get help from others
Good Luck!
0
SteveGTRCommented:
highmarks have you tried OpenEx() as annotated is my solution? Your initial example using Open(). I see where you made the comment about using the noodbcdialogbox, but not any code that uses it. If you are using this option how did you specify it (in code)?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.