I have a console app that needs to connect to a remote sql server 2008 instance....
this particular line throws an error even though i have access to that database...
connection.open() is the line that is throwing the error...
Make sure that Sql server 2008 instance available at your system where you are running your application.
Atleast client version of Sql server 2008 should be installed on development machine.
Check connection string and server system also allow network connection.
Make sure the connection string in you console app uses the same credentials and settings that you use to successfully connect in management studio.
Are you using Windows authentication in one place and a SQL login in another?
Have you been able to connect to this remote server before? If you have been able to connect previously, but cannot do so now, that may be an indication that a backup copy of the database, from another server, has been restored to that server.
When you write that you "have access" to that database, how do you connect? Close your copy of SQL Server Management Studio and re-open it; when you get the Connect dialog, be sure to enter the user credentials that your console app uses to connect. Do you see your database? Are you able to execute stored procedures, run SELECT statements, or otherwise do what you would expect?
If you do not see the database--or if you cannot do the activities you expect, you have probably restored a database from another server. This is very common--a database developer is working on the next revision of the database on his development machine; when it comes time to deploy he backs up the database, copies it to the server, and restores the database. Now he can't connect--and can't understand why.
The reason is that the Login and User are presented in the SSMS UI as strings--but in fact they are integer IDs. You will have to do a bit of scripting to delete the invalid User record from the database in order to assign it to the new server's Login.
If this sounds like your problem, respond--I'll check back in a bit, and follow up with more help.
JM
Related
I am trying to connect my .NET project to SQL Server developer edition 2019 (named instance), but it always throw the error:
Login failed for user 'sa'.
It however connects perfectly through SSMS without any issues.
I tried all possible things as suggested over internet but could not make my website run, is it actually the named instance which causes this issue, or it's issue at large.
I am able to connect my project if i connect to my database located on the remote server. but the one on my local machine just does not works.
Here's how my connection string looks like
server=[machine]\\[instance]; Integrated Security=SSPI; initial catalog=[database];Connection Timeout=1000
I also tried the same with SQL Server authentication
server=[machine]\\[instance]; uid=sa; pwd=[password]; initial catalog=[database];Connection Timeout=1000
None of the above worked out.
Ok, SSMS can connect using direct.
However, from asp.net, you in most cases have to turn on networking.
For reasons of secuirty, by default, your local instance of SQL server express has this feature turned off by default.
so, you need to do two things - and this was NOT required with older versions of sql server, but for recent versions - starting around 2012, you need to:
Turn on the browser service. This "translates" the sql "instance" to your IP address. (on startup, sql server assigns a IP address to that instance, but it will NOT be the same IP as your computer). But, the NAT translator thus has to be running.
That IP translator? It called the SQL browser service, and as noted, you need to have that running now, where as in the past you did not. That is this setting - launch the sql configuration, and you see this:
So, make sure the SQL Server Browser service is running - it is NOW required and as noted was not in the past.
Next up:
You need to turn on networking for SQL server. SSMS (sql studio) is able to connect using memory pipes - applications using the sql server provider in .net cannot!!!
So, once you ensured and checked the above browser service is running?
Then click on SQL server Networking Configuration, and turn on network connections (again, they are not turned on by default). That is this:
Now, after doing above, you probably should re-start sql server, and then the browser service. (just right click on sql server again, and choose this:
So, while SQL studio can connect using shared memory, .net and asp.net sites can't.
So, start the sql browser service - it is required to "resolve" the instance of sql server. in most cases, that will be SQLEXPRESS.
And consider using the built in connection builder in Visual Studio - it will not only build the connection string for you, but will also test the connection - and this can be done long before you even run any code.
I have a number of .NET web apps on a Server 2008 machine that I'm trying to migrate to a Server 2019 machine, and some of them are giving me problems connecting to SQL Server 2016 instance on another server after moving them.
The error I get is
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid) ---> System.ComponentModel.Win32Exception (87): The parameter is incorrect
This one is a .NET Core 2.2 web app. (.net core 2.2 server package installed on server) the connection string is like
"Server=mysqlserver\myinstance,3050;Database=Idea;Trusted_Connection=True;"
and is using Entity Framework/DbContext to connect.
The app in IIS has an app pool created for this app, set up the same as it was running on the old server: No Managed Code, Identity set as a domain user "domain\user".
Ping from new app server to database server works. Running SSMS as the domain user on the new server connects to the database fine and can view data through management studio.
So I don't know if there is something different in Server 2019 about the way it's trying to connect to SQL Server or what? I've been banging my head on this for a few days now. .NET 4.0 apps are also having the same errors trying to connect to other databases on this same instance.
The weird thing, is some other apps work fine, connecting to a different SQL 2016 instance on a different server, but they are also different .net versions, like older .NET 4.0 web apps, but they are running as app pools with different domain accounts for each app fine.
It seems all .NET Core or .net 4.0 web apps on this server are having trouble connecting to this one database instance from this server, but back on the old web app server they work fine.
Any ideas of anything else I can check?
Edit: I found the error is actually when connecting to a new SQL Server, even from the old app server it still gets the same error. I have 2 connections in this application, so I thought it was the first one, but it was actually the 2nd one. So it's something more to do with the new SQL server instance. Again, connections from SSMS work fine with this user, but not from the web app.
Edit2: After more testing, it's definitely something to do if there is Server 2019 in the mix. From 2019 app server to 2019 db server fails. From 2008 app server to 2019 db server fails. From 2019 app server to 2016 db server fails. From 2008 app server to 2016 db server succeeds.
Edit 3: I feel like I'm going crazy here. One of the apps, I tried to change ASPNETCORE_ENVIRONMENT to Development so that I could see more detailed errors on screen, so I set the appsettings.Development.json to the exact same as the appsettings.Production.json and then the connection works! Switching it back to production it gets the error again. The entire file is the exact same text. How does that even make any sense? I even tried explicitly setting the environment variable to Production instead of letting it just pick it up as the default.
Edit 4: I've solved half of the problems I've been having now.
For whatever reason, the connection string that I copied from one of the spreadsheets in the beginning had crazy hidden characters in it so that’s why it was saying the sql server didn’t exist.
I can’t see them at all in any editor and only found it by VS Code compare saying the line was different but not seeing any difference I broke it into chunks and found the spot. We found when we opened it in WordPad, that was the only place that would show it, see below.
I'm fairly sure the solution is to remove the instance name from the connection string...
"Server=mysqlserver,3050;Database=Idea;Trusted_Connection=True;"
Refer to these questions question1 & question2
"It's not necessary to specify an instance name when specifying the port."
Problem was due to hidden characters in the connection string. See my Edit 4. Other problems leftover were unrelated.
Try to create an user in the database using the same AppPool name that you used to configure your application in IIS.
Here's a "how to" create the user in the database:
https://engram404.net/grant-sql-permissions-to-iis-apppool-user/
It worked for me.
Otherwise here's an "why" it happends:
You're telling in your connectionString that it will be using an trusted connection "Trusted_Connection=True;"
If you do not want to create the user as described earlier, you should remove the Trusted_Connection=True; and use your connection string like this:
Server=mysqlserver\myinstance,3050;Database=Idea;User Id=SetYourUser;Password=SetYourPassword"
I know you happened to solve this, but still..
You can't connect to a MySQL database with System.Data.SqlClient - It is configured for an SQL database, not MySQL
You can find the MySQL Data Connector Here. (You can otherwise download it from Nuget)
If you downloaded it from Nuget, then skip this step.
After downloading the package, you can add it as a reference in your project, by right-clicking the References item in the Solution Explorer, then click Add Reference... - The Reference Manager window will then open. Click the Browse item in the left-menu, then click the Browse button, and navigate to the directory of which the package was saved.
Now, after successfully downloading and installing the package, add this line to your code:
using MySql.Data.MySqlClient;
The correct syntax for connecting to your MySQL Database, using MySql.Data.MySqlClient;, would be:
string connectionInfo = #"Server=localhost;Database=your_database;User ID=root;Password=123456";
So, overall, your code would look like:
using(MySqlConnection con = new MySqlConnection(connectionInfo))
{
con.Open();
MessageBox.Show("Successful Connection!");
}
(Code / part of answer is derived from here.)
I hope I could help anyone else with this problem :)
I have a windows form application (developed in VS Express 2013) that connects through an instance of SQL Server 2012 to a database. Both the application and database are on my local system; I just needed a GUI to more easily interface with this very large database that stores my research data. When I initially compile and deploy the application, it works fine and has no connection problems with the database. However, if I then attach the database in SQL Server Management Studio (which I sometimes want to do) I get an error the next time I try to use the application - "Cannot open database ..... requested by the login, the login failed". I get this error even if I take the database offline and detach it before quitting SSMS. And just to be clear - I'm not making any changes to the database in SSMS, I'm just looking at the data. The connection string used by VS is Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CollectionMetricsDatabase.mdf;Integrated Security=True.
Any idea what the problem is?
Maybe the instance of SQL Server 2012 has a max number of concurrent connections and you exceed that with your two connections at once (SSMS connection and application's connection). Check the settings in SSMS.
Another thing to try - Based on your connection string, you are using Integrated Security, which in my experience means the application connects using Windows credentials. That can be fragile, because it means that whatever 'user' runs the application (if hosted in IIS, this will be the App Pool user) is the one whose Windows credentials are used. I would suggest creating a SQL login user (in SSMS) for the database, with db_owner access, and then changing the application's connection to be username/password based instead. You may need to enable Mixed Authentication for the SQL Server instance in SSMS if it doesn't already allow it.
You need to connect in SSMS with the localDB connection string. Attaching it will prevent it from working with localDB.
In SSMS, create a connection to (LocalDB)\v11.0. You should see your DB there already.
The problem is that you are connecting to the database file directly not through SQL Server.
Only one user can have a lock on the file at the time.
You need to recreate a connection string for your app.
That is why you were needing to detach the database.
Since this is only a personal project you could live with things as they are, at least you know the server is only running when you need it.
create connection msdn
I've just start working on an ASP.NET Web Application in Visual Studio 2015 and SQL Server 2014. Somewhere, I need to connect to my local database and I use this:
ConnStr = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=my_db;Integrated Security=True";
It works fine when I run the application through VS in my browser. I can access the db and do whatever I want to do. But, when I publish my application in IIS Web Server, and then I open it in browser, it still works OK until I have to access the db. At that moment it throws SqlException:
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. .
Maybe it is a server name problem and I should use an IP and a Port instead of that name, but I'm not sure because I don't have experience on this.
You are correct: You should use server name and/or IP in your connection string.
Using something like "local" or "localhost" means that your code is not portable. Another option would be to store your connection strings in two separate config files - one for your local copy (for development and troubleshooting) and one for your server (for portability). I have two config files in my solutions:
Web.config
WebServer.config
Then, when I deploy to the server, I just delete Web.config and rename WebServer.config to Web.config. It's totally portable and you'll never have connection string troubles again!
Also noteworthy: you're not including credentials in your connection string, which means that you're using windows authentication when connecting to SQL server. When debugging through visual studio, the application will run as you - and if you have the needed permissions, it will work. However, when running in IIS, it won't be running as you (at least, it SHOULDN'T be) - so you could run into issues there, as well.
EDIT
This link might be useful for you: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
Your connection string should look like this:
Data Source=192.168.1.10,1433;Initial Catalog=YourDatabaseName;User ID=SqlUserName;Password=SqlPassword;Connection Timeout=60; Max Pool Size=3000;
(you can set max pool size and connection timeout to whatever you want - or omit them entirely.
Also, regarding your windows issues - you need to make sure that the windows account IIS is using has permissions to traverse your network and reach your SQL Server instance. That said, I suggest that you use a SQL account instead of windows authentication.
Since you are using integrated security in connection string you will have to modify the Identity of App pool under which your application is running.
The Identity will be your windows username and password.
To change the username and password you need to go the advance settings of the app pool and process model you can see identity where you can add your windows credentials
Hope this helps
There are two problems in you connection string:
"Integrated Security" means you are using the native windows system for authentication. Similar to opening SSMS on the database using your Windows password. IIS is now trying to connect to the database, and connection string is telling to use the process that IIS is running under.
You can create a non-windows user in SQL Server and put the credentials into the connection string. Or you can grant the IIS user DB privileges. Or you can a lot different things here, but theses are the easiest to get you moving.
THe second problem in the connection string is the data source. Is there SQL Server on you local machine? If so that's why it's not working. Try to run your app in VS but against the remote SQL Server. That should be your next step.
The problem was that I thought that SQL Server was installed automatically with VS or at least with SQL Server Management Studio. BUT NO. So, as far as I understand, till know I have not worked with a real SQL Server. When I checked SQL Server Configuration Manager there were nothing running at SQL Server Services and so I realized that I was missing something.
Then, I installed SQL Server Express and build my db there. Now it is working fine even when I publish it. The connection string is
Data Source=.\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
It can also be:
Data Source=localhost\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
I am new here to this forum so mind if I am asking a question already answered somewhere.
I have made an application in windows form, now I want to implement it onto another client PC. I published it and I can install it somewhere else fine, but the problem is that when there is a database involved I just cannot get it to work. I have tried everything I know, I attached the DB with the setup using the setup wizard creator but I cannot get it to work.
My main question is: How can I publish an application successfully, along with its DB and everything in working condition, without installing MS SQL on the client PC, if possible?
Typically, when using SQL Server, you are running in a client-server scenario, and SQL Server is the server portion. The connection string stored in app.config would point to the SQL Server, and the user would never need to know anything more about the database.
If your application is designed to be run outside of the network (i.e. 3rd party), you have a couple of options. First, if you want to use a full version of SQL Server, you can make a client installer and server installer. The server installer would be for setting up the server side (including the installation of SQL Server), and during the client setup, they would need to enter the name of the database server.
If the client has SQL Server Express installed, you can supply the database file, and do a direct connection to it rather than installing it inside SQL. However, if the client does not already have Express installed, this won't work.
Finally, SQL Server Compact Edition is designed for running on the client. You can do a "bin deploy" - that is, copy the SQL CE .dll files with your application, rather than installing the software, and the client can use the supplied database. If you haven't written the application for CE, this would require some rework.
you can also use sqlLite , it just need a bit of modification as it stated by Dave Simione