How to open a connection on the back end in C# to a local database - sql

I had a database server for school and since the semester is over I no longer am on this server. I want to take all my projects and put them on my website I've been working on. The problem is they have the school server connection. On the back end when I want to open a connection to my local database how would I do this? My connection looks like this
SqlConnection con = new SqlConnection("Data Source=LocalServerName;");
I get an error about the user, but my username for my local connection has a \ in it and that's an invalid key so I can't put my username in

Try this....
string connectionString = "Integrated Security=SSPI;Initial Catalog=master;Data Source=(local)"
SqlConnection con = new SqlConnection(connectionString);
You can change the value for Initial Catalog from "master" to the name of your database. I'm assuming you're using integrated security (you login to SQL Server with your Windows account).
If your login failed, look in SQL Server's error log for a failed login attempt. The error will give you more details about why the login attempt failed.
If you're logging in using a domain account - a.k.a., Windows authentication, a.k.a., Integrated Security - you'll include the following in your connection string:
Integrated Security=SSPI;
If you are connecting with a SQL Server username (username and password maintained by SQL Server and NOT by Windows or Active Directory). You'll use the following:
Persist Security Info=True;User ID=<username>;Password=<password>;
If you're not 100% sure how to create your connection string, try this trick:
Create a text file on your desktop named "test.udl"
Once created, double click on that file.
You can set all the properties of your connection string here.
When you're done, hit OK.
Then open the file in a text editor (e.g., notepad).
You'll see a connection string configured with the options you previously set. Copy and paste.

Related

How can I change the VB.NET application connection string after deployment?

I have written a VB.NET application and created a setup file to install the same on the target machine. An ODBC connection is used to connect to SQL Server instance through a DSN.
My development computer used Integrated Security whereas the target computer uses SQL Authentication which requires me to hard code the credentials in the connection string while building the setup file.
I have previously looked up for solutions, but they require one to define the credentials every time the application is run. I have seen an application that requests the credentials the first time it is run or if the connection is unsuccessful, but unfortunately could not retrieve the source code for the same.
Any guidance on similar lines would be helpful.
Add application settings of string type with user scope for the user name and password, lets say username & userpass. Let the User save these values the first time they run your app. Then just incorporate the My.Settings.username and My.Settings.userpass into your connection string.
To save the settings:
My.Settings.username = txt_user.text
My.Settings.Save()
This way the user can change the username & password if required without you having to update your code.

How do I get a VBScript to run on the machine where it is stored? Or, what am I doing wrong here? [duplicate]

This question already has answers here:
Windows authentication trusted connection not working
(3 answers)
Closed 6 years ago.
I wrote an SQL query which has to run on the server. To call that query I am using a VBScript which is stored on the server. When I run that script from the server, the script runs fine. But, when I run the VBScript from my local machine, I get an error stating that access was denied for the SQL script it is calling. I can't seem to figure it out. The following is the VBScript I am using:
dim dbconn, connect, ObjFSO
connect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=DBName;Server=ServerName;Database=DBName;ID=USERID;Password=PASSWORD"
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set dbconn = CreateObject("ADODB.Connection")
dbconn.Open connect
dbconn.execute ObjFSO.OpenTextFile("\\ServerName\FolderPath\FileName.sql").ReadAll,128
Set dbconn = Nothing
The error I get is:
Note: I do not get the error message seen in the image above, when I run the same VBScript from the server.
Is there a way to get this program to only run from the server where it is stored?
Am I just missing something really simple?
Perhaps you are logging into the server with one set of Windows credentials and logged into your desktop under a different username that doesn't have SQL access?
In any event, since the connection string you're using indicates SSPI security, it's going to try and use your Windows credentials to authenticate you, and completely ignore the username and password provided in the connection string.
Try something like this instead (see www.connectionstrings.com):
Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;
User Id=myUsername;Password=myPassword;
The issue was with the user ID I was attempting to use. I made a new user ID which does not use a windows authentication to access the DB and boom, it works!

Microsoft SQL Server 2014 Error: 18452? [duplicate]

My webpages are on secured server (https), and I am trying to connect the SQL Server 2008 Database, which is normal server.
I am writing connectionstring on page itself, not in web.config file. And I am getting following error:-
System.Data.SqlClient.SqlException: Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication.
Please help, how can I connect it, does I have to make some webservices for it.
my code is as below:
public void FillCity()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "integrated security=SSPI;data source=dev-fcb; user id=sa;password=password;"
+"persist security info=False;database=mediapro";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from StateCityMaster where IsActive='1' order by CityName", con);
DataSet ds = new DataSet();
da.Fill(ds);
string CityName = string.Empty;
if (ds.Tables[0].Rows.Count > 0)
{
CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
}
DataSet dset = new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count > 0)
{
drpCity.DataSource = dset;
drpCity.DataTextField = "CityName";
drpCity.DataValueField = "CityName";
drpCity.DataBind();
}
drpCity.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
Your connection string is telling it to use integrated security SSPI, which will use the Windows credentials.
Set Integrated Security to false if you are going to be providing the username and password.
Also, consider putting your connection string inside of the web.config file - it is more secure and reusable.
From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=VS.100).aspx:
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
I created a new Asp.Net Core MVC site and I had this same error. I was attempting to connect to my company's database while connected to the network via VPN. Here's what worked for me:
Instead of
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
I used
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
The key change was to make sure that Trusted_Connection=False, which is consistent with the error message. Generally I would not use an untrusted connection. In this case I was connecting to a dev/test database so it's fine.
For future googlers:
If you do need Integrated Security and are getting this error it might be you're using a local account instead of a domain account.
I came across this running Visual Studio locally and trying to connect to a database on another machine. A workaround was to run Visual Studio as a different user, the prompt didn't work but running the command below did (make sure to replace DOMAIN\USER and you will be asked to provide credentials):
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
(This is for VS2019, your path may vary).
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source set to 127.0.0.1. It worked fine for years.
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01) instead of 127.0.0.1 in the data source of the connection string.
I had this same issue while accessing this through work where we use Azure authentication - I'd changed my password through the Azure password reset service but this only pushed through after I manually updated the password on the schema settings in my RDBMS (in my case, DataGrip).

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication

My webpages are on secured server (https), and I am trying to connect the SQL Server 2008 Database, which is normal server.
I am writing connectionstring on page itself, not in web.config file. And I am getting following error:-
System.Data.SqlClient.SqlException: Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication.
Please help, how can I connect it, does I have to make some webservices for it.
my code is as below:
public void FillCity()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "integrated security=SSPI;data source=dev-fcb; user id=sa;password=password;"
+"persist security info=False;database=mediapro";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from StateCityMaster where IsActive='1' order by CityName", con);
DataSet ds = new DataSet();
da.Fill(ds);
string CityName = string.Empty;
if (ds.Tables[0].Rows.Count > 0)
{
CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
}
DataSet dset = new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count > 0)
{
drpCity.DataSource = dset;
drpCity.DataTextField = "CityName";
drpCity.DataValueField = "CityName";
drpCity.DataBind();
}
drpCity.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
Your connection string is telling it to use integrated security SSPI, which will use the Windows credentials.
Set Integrated Security to false if you are going to be providing the username and password.
Also, consider putting your connection string inside of the web.config file - it is more secure and reusable.
From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=VS.100).aspx:
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
I created a new Asp.Net Core MVC site and I had this same error. I was attempting to connect to my company's database while connected to the network via VPN. Here's what worked for me:
Instead of
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
I used
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
The key change was to make sure that Trusted_Connection=False, which is consistent with the error message. Generally I would not use an untrusted connection. In this case I was connecting to a dev/test database so it's fine.
For future googlers:
If you do need Integrated Security and are getting this error it might be you're using a local account instead of a domain account.
I came across this running Visual Studio locally and trying to connect to a database on another machine. A workaround was to run Visual Studio as a different user, the prompt didn't work but running the command below did (make sure to replace DOMAIN\USER and you will be asked to provide credentials):
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
(This is for VS2019, your path may vary).
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source set to 127.0.0.1. It worked fine for years.
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01) instead of 127.0.0.1 in the data source of the connection string.
I had this same issue while accessing this through work where we use Azure authentication - I'd changed my password through the Azure password reset service but this only pushed through after I manually updated the password on the schema settings in my RDBMS (in my case, DataGrip).

Mysql migration tool, source parameter string | connection string for sql server

I always got failed to "fetching of list failed error".
This is my connection string in ASP.NET
"Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;Integrated Security=SSPI"
What exactly should I input at MySQL migration tool for source parameter string
FYI,'maywood' is my computer name and I am using SQL Server 2000.
You have integrated Integrated Security=SSPI set -- perhaps you should try setting the UID and Password manually to values that you know are correct.
Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;
User ID=myUsername;Password=myPassword;
Integrated Security uses the credentials from the Windows system that you're using to log in -- these credentials might not be valid for the database you're trying to access.