locked
Connectionstring to a protected drive RRS feed

  • Question

  • I don't know if this is the right place where to post this question...

    I would like to connect oleDb to a xls file which is in a folder in a lan-drive..

    If i enter in that hard disk manually from windows using username and password i can connect to my excel file.

    But if i don't enter in that hard disk manually the connectionstring gives me an error...


    The connectionstring i use is this one:

    Connectionstring = @"Provider = Microsoft.Jet.OLEDB.4.0; DataSource = \\192.168.10.90\file.xls; Extended Properties = Excel 5.0"

    Is there any system to make a connection to that network drive with the username and the password and then use the connection string ?

    Thanks
    Wednesday, October 15, 2008 11:15 AM

Answers

  • You might consider whether entering the credentials into the Control Panel (User Accounts\Manage Network Passwords on Vista) would be acceptable.

    Another possibility is to shell out to the net.exe utility included with the OS.

    NET USE \\computer\share password /USER:username@something

    For domain users, something is the domain.  Otherwise, I think you can use the name of the machine or its IP address where the share is located.

    (If you are into PInvoke, the underlying API would be WNetAddConnection2.)

    Once you execute this command, your session will automatically be able to access the files on the share.  FYI, there is also a NET USE DELETE that you can use to terminate a session established by the above:

    NET USE \\computer\share /DELETE
    Wednesday, October 15, 2008 11:06 PM
  • > So i don't get it... if there is a connection now to a folder on a network drive, why if i go back to windows and try to open that folder it still asks me the username and the password....?

    This is not my experience with the NET USE command.  I see that you are using an IP address in your data source.  Did you specify that same IP address in the NET USE command?

    Another thing to watch out for is if your program runs as a service.  In that case, I think that the network sessions are separate and you will actually need to make sure your service is the one establishing this connection.


    Thursday, October 16, 2008 10:13 PM

All replies

  • You might consider whether entering the credentials into the Control Panel (User Accounts\Manage Network Passwords on Vista) would be acceptable.

    Another possibility is to shell out to the net.exe utility included with the OS.

    NET USE \\computer\share password /USER:username@something

    For domain users, something is the domain.  Otherwise, I think you can use the name of the machine or its IP address where the share is located.

    (If you are into PInvoke, the underlying API would be WNetAddConnection2.)

    Once you execute this command, your session will automatically be able to access the files on the share.  FYI, there is also a NET USE DELETE that you can use to terminate a session established by the above:

    NET USE \\computer\share /DELETE
    Wednesday, October 15, 2008 11:06 PM
  • Thanks a lot for the answer....

    I tried to use the dos shell to try thios net.exe command, and following your instruction i achieved a result

    "Execution correct" or something like that...

    So i don't get it... if there is a connection now to a folder on a network drive, why if i go back to windows and try to open that folder it still asks me the username and the password....?

    Surely there is some magical mechanism i still don't understand  :)  . .. .

    I'd like to connect to this excel file in this folder..
    - the program should check if there's already a connection to that folder and if it is not it should ask on a window the username and the password and the use these data to connect to that protected folder on the lan....

    thank you for your help....


    Thursday, October 16, 2008 8:58 AM
  • > So i don't get it... if there is a connection now to a folder on a network drive, why if i go back to windows and try to open that folder it still asks me the username and the password....?

    This is not my experience with the NET USE command.  I see that you are using an IP address in your data source.  Did you specify that same IP address in the NET USE command?

    Another thing to watch out for is if your program runs as a service.  In that case, I think that the network sessions are separate and you will actually need to make sure your service is the one establishing this connection.


    Thursday, October 16, 2008 10:13 PM
  • Yes, I did use the same ip local address also in the NET USE command....

    That address is the address of the network drive..
    so i use this command

    NET USE \\192.168.10.90\Public pwd USER:username

    (every person has got a different password and account for that drive...)

    and the answer of the dos shell was : Connection established

    But when i try to open that directory from window it asks me again username and pwd... (I thouhgt that the connection was established to a computer, not to a program))

    I barely understood that thing about the service... but yes, i think my program is a service so is should make that connection from that program...

    Thanks for the answer....

    Friday, October 17, 2008 8:14 AM