Program won't connect to SQL Server - sql-server

Update: This is fixed thanks to some great help here. If you're having similar issues do the following:
Enable ALL exceptions before running
Figure out the exact error you're getting when trying to connect
Make sure that all users on SQL side have a user assigned to a login for that specific database
I've got a very nice program I've made that I'm ready to start pushing out for testing. The problem is that when I coded all of this I simply used the Integrated Security which uses my windows login to connect to the SQL Server. That worked fine as I'm a sysadmin on that whole server, including the database in question.
Now I'm ready to push this out to other users and I need to change the code to either use my login for all users (hard coded to the connection string) or use a new login specifically for that database.
I've used the following connection string and it won't work! I've also added the server to the data connections section of the server explorer but I guess I don't know how to tell the program to use that connection in my code.
I've obfuscated the password in the code below but it's written out in my code. No errors or anything when running - it just doesn't connect or pull data.
Thoughts? Thanks!
Dim strSQLConn As String = ("Server=Hertz1455;Database=AbsenceApplication;User ID=hac0421;Password=********")
Update - hertz1455 is the server name, there isn't a port that I need to use. Below is the rest of the code for when the program starts. I've also commented on some answers with the error I'm getting.
Dim strSQLConn As String = "Fleet_PTO.My.MySettings.AbsenceApplicationConnectionString"
Dim strQuery As String
Dim sqlSQLCon As SqlConnection
sqlSQLCon = New SqlConnection(strSQLConn)
Dim cmd As SqlCommand = New SqlCommand("SELECT person FROM tblSupervisor", sqlSQLCon)
sqlSQLCon.Open()
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim myDataTable As DataTable = New DataTable
myDA.Fill(myDataTable)
sqlSQLCon.Close()
When I change the strSqlConn to the string below, everything works just fine.
Dim strSQLConn As String = ("Data Source=Hertz1455;Initial Catalog=AbsenceApplication;Integrated Security=True")

We can only guess since the question is a bit unclear without any exceptions or warning messages.
Try this:
Create a new text file anywhere. (say Desktop)
Rename the extension to udl and open the file.
You should be seeing window.
Test your connection, see if you can connect.
Lat94 might be correct with the Server=Hertz:1455 part although it's not the default MSSQL port.
Also check your server's firewall, it might be blocking the connection request. You might be getting an exception (like connection forcefully rejected) but it might not be popping up. Check exception settings, enable everything since you find a relative clue.
Note:
Dear future viewer, please follow the comments.

Perhaps you should add the driver you're using in order to get it done, like:
Dim strSQLConn As String = ("Server=SQLOLEDB;Server=Hertz1455;Database=AbsenceApplication;User ID=hac0421;Password=********")
Is "1455 at Server=Hertz1455 the port number? Shouldn't it be Server=Hertz:1455 in that case?
Check this link for more info. It is in C#, but it will not be a problem.

Related

Flexible Connection String

This connection always fails when I transfer my system to another computer. Whatis the correct code for a connection string that can run on another computer?
Public Sub connect()
con = New SqlConnection("data source=.\SQLEXPRESS; Integrated Security=true; User Id=sa;Password=sa;")
con.Open()
End Sub
I'm expecting for the code that can be run on other PC when I transfer my system
That connection string will work on any machine that has a local instance of SQL Server Express with the default name. There's no way to build a connection string that will magically work with any SQL Server instance with any name on any machine. If each user may need a different instance name then you have to provide for that.
One option is to store your connection string in the config file and let each user edit that by hand. That's risky if your user's aren't technically inclined though. You can also store the connection string attribute values somewhere, e.g. My.Settings and then provide a UI for the user to specify them at run time and persist them for later use. The specifics of that are up to you but you can find an example here:
http://www.vbforums.com/showthread.php?532768
The point of that thread was the encryption part but you can ignore that and just use the rest.

Error deploying my visual studio project in another computer

I had a deployed visual project 2010 connected to sql and I had installed it in another computer but the database can't connect to the final project. I want to install it in other computers that will be connected to database. I think I have a problem in my connection:
Dim con As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=enrollment")
The information provided at this link will provide you with a good idea of how to manage connection strings in VS. The problem with your SqlConnection object is that your data source is still listed as 'localhost'. You need to change this to point to the location of your database.
Ideally the application should have a user friendly way of specifiying the settings for the connection, this will make connecting from different locations far easier and makes your application more portable.
I think this example here will help you

Why does sqlConnection.Close() not close the login?

We have unit tests to test that our database install and uninstall features successfully work.
The unit tests use the SqlClient.SqlConnection class to check the database contents before, during and after.
Our problem is that after using SqlClient.SqlConnection, the drop login part of the uninstall fails because it claims that a user is currently logged in. Even though we have called SqlConnection.Close(), the login seems to be still open.
Our code looks a little like this:
InstallTables(); // function uses smo to create tables in a database.
string connString = CreateLogin("userName", "password"); // create login with smo
// Test the returned connection string connects to the database
using (SqlConnection con = new SqlConnection(connString))
{
con.Open();
//test code to read the DB version out of a table
//Dispose calls con.Close() - I have also tried calling it explicitly
}
DropTables(); // uses smo to drop tables from the database
DropLogin("userName", "password"); // uses smo to drop the login
The DropLogin fails with the following exception:
System.Data.SqlClient.SqlException: Could not drop login 'engageSecurity_unittest_129418264074692569' as the user is currently logged in.
If I remove all the SqlConnection code until after the DropLogin, then everything runs fine.
Does anyone know why the user is not logged out when I call SqlConnection.Close() ?
Is this something to do with connection pooling?
Unless you're explicitly disabling Connection Pooling in your connection string, my guess is that even though you're disposing the connection it is still alive in the Connection Pool (in case you decide to re-use it):
SQL Server Connection Pooling (ADO.NET)
Try disabling Connection Pooling (by adding Pooling=false; in your connection string) and see what happens.
Alternatives to Pooling=false are SqlConnection.ClearPool and SqlConnection.ClearAllPools Method.

mobile application cannot see remote sql server?

I am sorry for bothering you with my probably low level question.
I would like to develop a very very very simple application which will connect the MS SQL SERVER in my notebook and will be run at hand terminal-with windows mobile 6.
Here is the code which i used :
Dim strSQL As [String] = "SELECT COUNT(studentid) AS totalp from Students where gender='m' "
' Dim Conn As New SqlConnection("Data Source=10.0.0.4\MUSTAFASQL2;Initial Catalog=MYDBX;User ID=TEACHERA") <<< i used this too,
' Dim Conn As New SqlConnection("Data Source=10.0.0.4:1433\MUSTAFASQL2;Initial Catalog=MYDBX;User ID=TEACHERA;") <<< and also tried this
Dim conn As New SqlConnection("Data Source=MUSTAFA\MUSTAFASQL2;Initial Catalog=MYDBX;User ID=TEACHERA;")
Conn.Open() *<<<<<<<<< crashes here with this error : "Specified SQL server not found : MUSTAFA\MUSTAFASQL2"*
Dim cmd As New SqlCommand(strSQL, Conn)
Dim musreader As SqlDataReader = cmd.ExecuteReader
While musreader.Read
total.Text = musreader.Item("totalp").ToString
End While
Conn.Close()
on button click.
Some details :
Device : Windows Mobile 6.1 classic
Local : SqlExpress and i can reach from many computers, i mean there is no problem accessing remotely to my SQL Server.
Please help me, all helps are appreciated. Thanks a lot.
Please let me know if you need to know more details for help...
It seems problem not in your code but in connection to 10.0.0.4 server.
If the other computers are on the same network then they will be able to see your SQL Server instance. However, if the mobile device isn't (which I wouldn't have thought it would be) then it won't.
The IP address 10.0.0.4 (which I see you've tried to use in the commented out connect statements) is an internal address which won't be visible to the wider internet.
You need to put your database on an external facing server with a public IP address and connect to that.

Classic ASP - SQL Server 2008 Connection String using Windows Authentication

This should be painfully simple, but I cannot come up with a working connection string for a local copy of SQL Server 2008 using Windows Authentication. I've tried using the Data Link Properties tool to create a connection string and it has no problems connecting, but when I copy paste the generated string into my ADODB.Connection object's ConnectionString property I get all sorts of fun and different errors.
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=climb4acure;Data Source=(local);"
Microsoft OLE DB Service Components (0x80040E21)
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
I've tried a variety of similar connection strings but I cannot find one that will work with Windows Authentication. Can someone point me in the right direction?
Thanks!
Here's an easy way to generate connection strings that work.
Right-click an empty spot on the desktop and choose NEW, TEXT DOCUMENT from the context menu
Save it with a .udl extension, and click yes when it asks are you sure.
Double-click the new udl file you just created. It will open a dialogue. Go to the Provider tab, and choose the appropriate provider.
Go to the Connection tab and fill in the server name and database name, and choose NT authentication (or use a specific username and password, which is SQL authentication). Now click Test Connection. If it works, you're ready to click OK and move on to the final step. If it doesn't you need to resolve permission issues, or you've mis-typed something.
Now right-click the file on the desktop and open it in notepad. It will display the connection string that you can copy and paste to wherever you need it.
I assume you have the 2008 Native Client installed? Also, I noticed that you're missing the "provider" tag at the beginning - do you have any more luck with this one:
Provider=SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=climb4acure;Data Source=(local);
Have you had a look at connectionstrings.com? They are a pretty good reference (but, in my experience, they don't work too well in the Google Chrome browser).
Works absolutely fine:
"Provider=SQLNCLI;Server=xxxxxxxx;uid=sa;pwd=xxxxxx;database=xxxxxx;"

Resources