Connection failed!

Hi guys,

I have problem with this code:

TCHAR szCurrentDir[250];

TCHAR szDataDir[250];

lstrcpy(szDataDir,

TEXT("DRIVER={SQL Native Client};SERVER=.\\sqlexpress;Trusted_Connection=Yes;DATABASE=MyDic;AttachDBFileName="));

GetCurrentDirectory(sizeof(szCurrentDir), szCurrentDir);

lstrcat(szCurrentDir, TEXT("\\Database\\Data.mdf;"));

lstrcat(szDataDir, szCurrentDir);

SQLDriverConnect(hdbc, hWnd, szDataDir, SQL_NTS, szConnStrOut, sizeof(szConnStrOut), &cbConnStrOut, SQL_DRIVER_COMPLETE);

When I run my program, I get this message:

---------------------------
Microsoft SQL Server Login
---------------------------
Connection failed:
SQLState: '42000'
SQL Server Error: 5133
[Microsoft][SQL Native Client][SQL Server]Directory lookup for the file "C:\Program Files\MyDic\Database\Data.mdf" failed with the operating system error 5(Access is denied.).
Connection failed:
SQLState: '42000'
SQL Server Error: 1832
[Microsoft][SQL Native Client][SQL Server]Could not attach file 'C:\Program Files\MyDic\Database\Data.mdf' as database 'MyDic'.

---------------------------
OK  
---------------------------

I have to mention that:

SQL Server: SQL Server (2005) Express

Operating System: Windows XP SP2

and I'm Local Admin.

I wanna deploy this program with data.mdf file that contains program data.

So, How can I fix it

Regards

 




Answer this question

Connection failed!

  • Yuri55

    Dear Markus,

    Thank you so much. I got that.

    Regards.



  • Derry1406

    That's a tough one. I guess the easiest way would be to create an 'sa' login on your SQL Server installation, and then replace 'Trusted_Connection' with 'User=sa;Pwd=<<WhateverPasswordYouCreated>>;'.

    This would guarantee you have admin rights to attach/detach databases.

    Second easiest would be to log onto machine as admin, and add your user name as a machine admin. (I’m afraid I’m not too good at showing how to do that on an XP machine, since I only use W2K3 Server.)

    The more difficult method to do this programmatically would be to use “Runas”. I’m not positive it would work, but you could try running your application from the command line via ‘Runas admin’ which would give it admin rights, and it should work. Please see http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx mfr=true for more documentation on how to use this.

    If anyone else has suggestions, please speak up!

    Good luck!

    ~Warren


  • AbirB

    I don't think you need to change the service account programmatically. You can just give the read/write permission to the service account. Do you have any special reason that you don't want to give the permission to the SQL service account on your computer

    Thanks.



  • Sutapa

    Does C:\Program Files\MyDic\Database\Data.mdf exist Does the service account for your sqlexpress has permissions to read/write this file You must have at least read permission to the file to logon to the server. (You cannot relay on sqlexpress create the file for you.)

  • robertcabiyaan

    Hi Warren,

    Thank you for your comments.

    By the way, I read this article:

    "How to verify and change the system administrator password in MSDE or SQL Server"

    http://support.microsoft.com/Default.aspx scid=kb;en-us;322336&spid=2855&sid=global

    But when I created sa, sqlcmd said that sa account disabled. Do you know how I can enable it

    Thanks.



  • Charles Pich&amp;#233;

    Dear Xinwei,

    I think you are right. But how can I change the service account programmatically in order to have read and write permission

    Thanks,



  • Ffee

    Dear Warren,

    I have checked all things you mentioned, but it doesn't work

    I think this issue is concerned with the service account for my SQLEXPRESS as Xinwei Hong mentioned.

    Well, Do you know how I can change this account programmatically so that I could get rid this issue

    Best



  • lichtkind

    Curious issue. It looks like your connection string is correct. Can you check to see if your detached database's assocated .LDF file is read-only (I had that issue before.) If it doesn't have one, then ignore this suggestion.

    Another thing you might try is to see if putting your detached database into the SQL Server's 'Data' folder. Something along the lines of 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data'. Again, probably not the issue since you're getting an 'access denied' error, not one about 'file not found'.

    Did you detach with same version SQL server This could be a compatibility issue.

    It almost sounds like you're trying to attach a database that already exists. Could this be possible

    Try to debug your issue with the following steps:

    1) Create a database, with tables, etc...

    2) Detach the database

    3) drop the database

    4) try to re-attach the database using a connection string like the one above.

    If you can get this to work then you should be okay.

    If you're still stuck, re-activate this issue and I'll try to think of something else.

    ~Warren


  • Payton Byrd

    Hi Xinwei,

    As I said before, I just wanna deploy my program containing data.mdf file. I made installation and I tested but I got this problem. I read some technique to deploy MDF files such az Xcopy, but it didn't work.

    sincerely.



  • Vitor

  • Connection failed!