Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cannot automatically connect to SQL Server 2005

Author  Topic 

magmalloy
Starting Member

2 Posts

Posted - 2011-01-05 : 06:44:27
Using VS 2008 and SQL Server 2005, I'm having problems connecting automatically to a database using the following C++ MFC application code written into an Event Handler:

CDatabase db;
CString s;
// Test with SQL Server
if (db.Open(NULL, FALSE, FALSE, _T("ODBC;Driver={SQL Native Client};Server=***;Database=C:\\Users\\Dave\\Desktop\\TestDB3.mdf;Trusted_Connection=yes;")))
{
s.Format(_T("INSERT INTO Testing123 (ID, M_WCURRENT) VALUES (%d, 'Wcurrent %.3f')"),
1, wcurrent);
db.ExecuteSQL(s);
}

The insert into the test table is included to demonstrate that commands are being sent, which do work, but every time an insertion is attempted there are error messages which prompt me to select the database manually. I need to write code so that the database will update autonomously every few seconds so obviously I need to fix this somehow, but I have been trying everything I can think of with no success.

I get two error messages with the above attempt at a trusted connection:

- connection failed, sqlstate '08004', server rejected the connection, access to selected database has been denied; and

- connection failed, sqlstate '28000', sql server error 18456, login failed for user

and if I try a user id connection I get:

- connection failed, sqlstate '28000', sql server error 18456, login failed for user id

However, after the error message I get a pop-up which allows me to connect IF I choose trusted connection and also select the database manually (even though the correct database is already there in the default pop-up field, it seems access is only granted through the pop-up).

I've looked into permissions and server roles through the SQL Server Security/ Login/ Properties and given permissions everywhere possible, but nothing has worked. It doesn't make sense to me that I can connect but only manually through a pop-up.

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-05 : 08:33:18
Well...the connection string you posted points to a database data file, not the database itself. Maybe if you try to change "Database=C:\\Users\\Dave\\Desktop\\TestDB3.mdf;" into "Database=TestDB3;" it will work better for you.

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

magmalloy
Starting Member

2 Posts

Posted - 2011-01-05 : 08:44:11
quote:
Originally posted by Lumbago

Well...the connection string you posted points to a database data file, not the database itself. Maybe if you try to change "Database=C:\\Users\\Dave\\Desktop\\TestDB3.mdf;" into "Database=TestDB3;" it will work better for you.

- Lumbago




Wow I've tried lots of variations for the connection string but not that particular one, and it worked! My god it was such a small detail!

This has saved me perhaps a couple of weeks researching and writing alternative methods of implementing the database code. Sounds crazy for what I now know to be something so small and simple but there it is.

Thanks from the bottom of my SQL-Server-noob heart sir.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-05 : 09:07:03
Excellent! I'll go ahead and write out that invoice for a couple weeks research that you saved

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -