Cannot connect to SQL Server via VB.net but I can login to the server from SSMS with the same cridentials












1















I'm working on an application in VB which has to connect to a SQL Server 2014 Express database with the following info:



DBName: testDB
DBHost: USER-PCSQLEXPRESS
DBUser: testuser
DBPass: testpass


and the following code to make the connection:



Public Shared Sub connect(DBName As String, DBHost As String, DBUser As String, DBPass As String)

Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
builder.DataSource = DBHost
builder.UserID = DBUser
builder.Password = DBPass
builder.InitialCatalog = DBName

connection = New SqlConnection(builder.ConnectionString)
connection.Open()

End Sub


I'm able to login normally with this information (using SSMS) but not with the code here. Now I know that this code works as I've used it on a different machine where it worked fine. In both cases the server is hosted locally. I've enabled all protocols in the Network Configuration but I'm still not able to login via this code. Also I set the login to also work with SQL login.



I'm printing the exception it gives me and it sais




Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'.




Why does it allow me to login via SSMS and not via my program?










share|improve this question

























  • What do the logs in SQL Server say? Can you see the connection?

    – Larnu
    Nov 22 '18 at 19:25











  • @Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

    – BRHSM
    Nov 22 '18 at 19:29











  • Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

    – Dave Cullum
    Nov 22 '18 at 19:32






  • 1





    Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

    – StepUp
    Nov 22 '18 at 19:35






  • 1





    @StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

    – BRHSM
    Nov 22 '18 at 20:13
















1















I'm working on an application in VB which has to connect to a SQL Server 2014 Express database with the following info:



DBName: testDB
DBHost: USER-PCSQLEXPRESS
DBUser: testuser
DBPass: testpass


and the following code to make the connection:



Public Shared Sub connect(DBName As String, DBHost As String, DBUser As String, DBPass As String)

Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
builder.DataSource = DBHost
builder.UserID = DBUser
builder.Password = DBPass
builder.InitialCatalog = DBName

connection = New SqlConnection(builder.ConnectionString)
connection.Open()

End Sub


I'm able to login normally with this information (using SSMS) but not with the code here. Now I know that this code works as I've used it on a different machine where it worked fine. In both cases the server is hosted locally. I've enabled all protocols in the Network Configuration but I'm still not able to login via this code. Also I set the login to also work with SQL login.



I'm printing the exception it gives me and it sais




Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'.




Why does it allow me to login via SSMS and not via my program?










share|improve this question

























  • What do the logs in SQL Server say? Can you see the connection?

    – Larnu
    Nov 22 '18 at 19:25











  • @Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

    – BRHSM
    Nov 22 '18 at 19:29











  • Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

    – Dave Cullum
    Nov 22 '18 at 19:32






  • 1





    Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

    – StepUp
    Nov 22 '18 at 19:35






  • 1





    @StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

    – BRHSM
    Nov 22 '18 at 20:13














1












1








1








I'm working on an application in VB which has to connect to a SQL Server 2014 Express database with the following info:



DBName: testDB
DBHost: USER-PCSQLEXPRESS
DBUser: testuser
DBPass: testpass


and the following code to make the connection:



Public Shared Sub connect(DBName As String, DBHost As String, DBUser As String, DBPass As String)

Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
builder.DataSource = DBHost
builder.UserID = DBUser
builder.Password = DBPass
builder.InitialCatalog = DBName

connection = New SqlConnection(builder.ConnectionString)
connection.Open()

End Sub


I'm able to login normally with this information (using SSMS) but not with the code here. Now I know that this code works as I've used it on a different machine where it worked fine. In both cases the server is hosted locally. I've enabled all protocols in the Network Configuration but I'm still not able to login via this code. Also I set the login to also work with SQL login.



I'm printing the exception it gives me and it sais




Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'.




Why does it allow me to login via SSMS and not via my program?










share|improve this question
















I'm working on an application in VB which has to connect to a SQL Server 2014 Express database with the following info:



DBName: testDB
DBHost: USER-PCSQLEXPRESS
DBUser: testuser
DBPass: testpass


and the following code to make the connection:



Public Shared Sub connect(DBName As String, DBHost As String, DBUser As String, DBPass As String)

Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
builder.DataSource = DBHost
builder.UserID = DBUser
builder.Password = DBPass
builder.InitialCatalog = DBName

connection = New SqlConnection(builder.ConnectionString)
connection.Open()

End Sub


I'm able to login normally with this information (using SSMS) but not with the code here. Now I know that this code works as I've used it on a different machine where it worked fine. In both cases the server is hosted locally. I've enabled all protocols in the Network Configuration but I'm still not able to login via this code. Also I set the login to also work with SQL login.



I'm printing the exception it gives me and it sais




Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'.




Why does it allow me to login via SSMS and not via my program?







sql-server vb.net sql-server-2014






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 19:33









marc_s

580k13011191266




580k13011191266










asked Nov 22 '18 at 19:20









BRHSMBRHSM

2822529




2822529













  • What do the logs in SQL Server say? Can you see the connection?

    – Larnu
    Nov 22 '18 at 19:25











  • @Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

    – BRHSM
    Nov 22 '18 at 19:29











  • Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

    – Dave Cullum
    Nov 22 '18 at 19:32






  • 1





    Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

    – StepUp
    Nov 22 '18 at 19:35






  • 1





    @StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

    – BRHSM
    Nov 22 '18 at 20:13



















  • What do the logs in SQL Server say? Can you see the connection?

    – Larnu
    Nov 22 '18 at 19:25











  • @Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

    – BRHSM
    Nov 22 '18 at 19:29











  • Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

    – Dave Cullum
    Nov 22 '18 at 19:32






  • 1





    Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

    – StepUp
    Nov 22 '18 at 19:35






  • 1





    @StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

    – BRHSM
    Nov 22 '18 at 20:13

















What do the logs in SQL Server say? Can you see the connection?

– Larnu
Nov 22 '18 at 19:25





What do the logs in SQL Server say? Can you see the connection?

– Larnu
Nov 22 '18 at 19:25













@Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

– BRHSM
Nov 22 '18 at 19:29





@Larnu I got the log file and found a whole bunch of these: 11/22/2018 19:17:21,Logon,Unknown,Login failed for user 'testuser'. Reason: Failed to open the explicitly specified database 'TestDB'. [CLIENT: <local machine>] I guess this is from my testing to login from the program itself..

– BRHSM
Nov 22 '18 at 19:29













Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

– Dave Cullum
Nov 22 '18 at 19:32





Does that database (TestDB) exist? Does that user (TestUser) have access to the database? You say you can logon with SSMS -- can you execute the command USE TestDB; in SSMS without error using those credentials?

– Dave Cullum
Nov 22 '18 at 19:32




1




1





Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

– StepUp
Nov 22 '18 at 19:35





Try to vary this row: Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=SSPI;") OR Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder("Integrated Security=true;")

– StepUp
Nov 22 '18 at 19:35




1




1





@StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

– BRHSM
Nov 22 '18 at 20:13





@StepUp It works! But I'm wondering how I was able to connect to the DB with the same code on a different machine with the same setup?

– BRHSM
Nov 22 '18 at 20:13












1 Answer
1






active

oldest

votes


















0














Try to vary this row:



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=SSPI;")


OR



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=true;")


What this means:



Integrated Security = False : User ID and Password are specified in the connection.



Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.



Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.



Please, see connection strings at MSDN.



Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:




  • you've written incorrect login or user of database TestDB

  • user testuser does not exist in your database


Run to see whether testuser exists in your database:



USE YourDatabase
SELECT * FROM sysusers


In addition, be sure that you've set a correct password of database user.



Update by Nick.McDermaid:



I was able to detirmine that I need to grand acess to the database for that user!






share|improve this answer





















  • 1





    Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

    – BRHSM
    Nov 23 '18 at 7:23






  • 1





    ..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

    – Nick.McDermaid
    Nov 26 '18 at 4:42











  • @Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

    – StepUp
    Nov 26 '18 at 7:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437000%2fcannot-connect-to-sql-server-via-vb-net-but-i-can-login-to-the-server-from-ssms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try to vary this row:



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=SSPI;")


OR



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=true;")


What this means:



Integrated Security = False : User ID and Password are specified in the connection.



Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.



Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.



Please, see connection strings at MSDN.



Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:




  • you've written incorrect login or user of database TestDB

  • user testuser does not exist in your database


Run to see whether testuser exists in your database:



USE YourDatabase
SELECT * FROM sysusers


In addition, be sure that you've set a correct password of database user.



Update by Nick.McDermaid:



I was able to detirmine that I need to grand acess to the database for that user!






share|improve this answer





















  • 1





    Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

    – BRHSM
    Nov 23 '18 at 7:23






  • 1





    ..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

    – Nick.McDermaid
    Nov 26 '18 at 4:42











  • @Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

    – StepUp
    Nov 26 '18 at 7:44
















0














Try to vary this row:



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=SSPI;")


OR



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=true;")


What this means:



Integrated Security = False : User ID and Password are specified in the connection.



Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.



Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.



Please, see connection strings at MSDN.



Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:




  • you've written incorrect login or user of database TestDB

  • user testuser does not exist in your database


Run to see whether testuser exists in your database:



USE YourDatabase
SELECT * FROM sysusers


In addition, be sure that you've set a correct password of database user.



Update by Nick.McDermaid:



I was able to detirmine that I need to grand acess to the database for that user!






share|improve this answer





















  • 1





    Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

    – BRHSM
    Nov 23 '18 at 7:23






  • 1





    ..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

    – Nick.McDermaid
    Nov 26 '18 at 4:42











  • @Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

    – StepUp
    Nov 26 '18 at 7:44














0












0








0







Try to vary this row:



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=SSPI;")


OR



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=true;")


What this means:



Integrated Security = False : User ID and Password are specified in the connection.



Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.



Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.



Please, see connection strings at MSDN.



Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:




  • you've written incorrect login or user of database TestDB

  • user testuser does not exist in your database


Run to see whether testuser exists in your database:



USE YourDatabase
SELECT * FROM sysusers


In addition, be sure that you've set a correct password of database user.



Update by Nick.McDermaid:



I was able to detirmine that I need to grand acess to the database for that user!






share|improve this answer















Try to vary this row:



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=SSPI;")


OR



Dim builder As SqlConnectionStringBuilder = New 
SqlConnectionStringBuilder("Integrated Security=true;")


What this means:



Integrated Security = False : User ID and Password are specified in the connection.



Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.



Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.



Please, see connection strings at MSDN.



Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:




  • you've written incorrect login or user of database TestDB

  • user testuser does not exist in your database


Run to see whether testuser exists in your database:



USE YourDatabase
SELECT * FROM sysusers


In addition, be sure that you've set a correct password of database user.



Update by Nick.McDermaid:



I was able to detirmine that I need to grand acess to the database for that user!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 7:41

























answered Nov 23 '18 at 7:18









StepUpStepUp

8,28284475




8,28284475








  • 1





    Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

    – BRHSM
    Nov 23 '18 at 7:23






  • 1





    ..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

    – Nick.McDermaid
    Nov 26 '18 at 4:42











  • @Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

    – StepUp
    Nov 26 '18 at 7:44














  • 1





    Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

    – BRHSM
    Nov 23 '18 at 7:23






  • 1





    ..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

    – Nick.McDermaid
    Nov 26 '18 at 4:42











  • @Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

    – StepUp
    Nov 26 '18 at 7:44








1




1





Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

– BRHSM
Nov 23 '18 at 7:23





Thanks for the info. i was able to detirmine that I need to grand acess to the database for that user!

– BRHSM
Nov 23 '18 at 7:23




1




1





..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

– Nick.McDermaid
Nov 26 '18 at 4:42





..as I said in my comment. The answer is not to change the authentication type, it is to grant the user access.

– Nick.McDermaid
Nov 26 '18 at 4:42













@Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

– StepUp
Nov 26 '18 at 7:44





@Nick.McDermaid yeah, you are right! I've updated my answer. If you want to make an answer, feel free to do it as you were the first.

– StepUp
Nov 26 '18 at 7:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437000%2fcannot-connect-to-sql-server-via-vb-net-but-i-can-login-to-the-server-from-ssms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]