Problem with SQL Server - c#

I am having a problem with accessing SQL Server in visual studio. I am reading in a csv file and putting it into a SQL Server database. I am using DBAccees to do this, however, when I run the application, I get a "Login Failed for user" error. I am not sure why it is doing this as the username and password is right.
DBAccess _dba = new DBAccess("server=servername;database=name;password=password;user=username");
Any help is appreciated

I think the username parameter in the connection string is wrong.
change it to:
DBAccess _dba = new DBAccess
("server=servername; database=name;password=password;uid=username");

If you cant get SQL server authentication to work, have you verified you have a correctly configured SQL user setup? With access to the correct database? If you don't need to use SQL authentication and want to use Integrated use this for a connection string:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Related

SQL Server Express Connection String Error in C#

I want to run a small C# program. Its database is on my local host in SQL Server. Now I want to change it to an SQL Server Express one. I have made a database which its name is exactly same with the one in local host, I have made the tables too.
Now I want to change the connection string. What syntax should I use?
I have used this one:
Server = localhost\SQLEXPRESS; Database = MyStore_DB; Trusted_Connection = true
I have used it in MyStore.App.exe.config and MyStore.DataLayer.dll.config files instead of the previous connection string in part ConnectionStrings.
But an error occurs. Could you please help me if I'm mistaken in any step?
error:
the picture of the error occured
Your connection string it's wrong. Once you've declared TrustedConnection=true (you miss the ';' at the end) it means that you will login into the database using Windows Authentication.
Beyonds that, if you wanted to login with an specific user and password, you may try this:
Server=.\\SQLEXPRESS;Database=YourDB;MultipleActiveResultSets=true;User Id=yourUser;Password=yourPassword;
Notice I'm using "." instead of localhost

Windows Service SQL Server error - "Login failed for user"

I have a Windows Service written in C# being run on a Windows Server 2012 R2 machine which connects to a remote SQL Server 2012 instance also on a Windows Server 2012 R2 Server.
The SQL Connection String is defined in the App Config as follows:
<connectionStrings>
<add name="destinationConnectionString" providerName="System.Data.SqlClient"
connectionString="Server=10.42.42.10;Database=mydb;User ID=myuser;Password=mypass" />
</connectionStrings>
When running the service, the output error log shows:
System.Data.SqlClient.SqlException (0x80131904): Login failed for user
'myuser'.
Error Number:18456, State:1, Class:14
So I check the SQL Server Logs to find out more information about the error and find this
Login failed for user myuser Reason: Password did not match for the
login provided"
Source: Logon Message: Error: 18456, Severity: 14, State: 8.
The problem is I know these credentials are correct. I've checked and double checked.
I am able to prove the credentials work by opening SQL Server Management Studio on the source machine, and connecting to the remote SQL Server using the exact same Server, User ID and Password with "SQL Server Authentication" authentication mode.
In fact I have 2 different users that I'm able to connect with using SSMS, but that fail using the Windows service. If I am able to connect to the remote machine using mixed mode auth directly in SSMS. Why not in my Windows service?
I'm at a loss here, could anyone suggest what the issue might be?
Edit: I can even successfully connect to the remote machine using the following command:
sqlcmd -S 10.42.42.10 -U myuser -P mypass -d mydb
The proper syntax for specifying the initial database is:
Server=10.42.42.10;Initial Catalog=mydb;User ID=myuser;Password=mypass
My best guess is that it is ignoring the "Database" in your string and possibly trying to connect to the default database setting for the login (possibly master maybe?) and doesn't have at least public role to it. Although I've found out that "Database" is a valid alternative.
It's possible there is a character in your password that is tripping it up. Semicolons and equal signs will be particularly problematic. Try putting double quotes around your User ID and Password like:
Server=10.42.42.10;Initial Catalog=mydb;User ID="myuser";Password="mypass"
If there's a quotation mark in your password you will have to replace it with two of them for each occurrence.
Once you get your connection string built, then you have to ensure that it will parse properly in XML. Which means escaping control characters and the other special characters such as:
< with <
> with >
& with &
' with &apos;
" with "
If you open up your app config file in a browser it should show you if it looks proper.
I recommend adding the contents of your entire connection string to our output log of your service. That way you can see what it looks like. At the point before you attempt to open the connection, get it with:
string connStr = ConfigurationSettings.AppSettings("myConnectionString");
Then output the connStr value to the log. It will show you what values are being used at least.
You might use some special character in password, that is reserved for XML. If so, you'll need to use escape character as a replacement.
For instance, if your password is PASS<WORD the valid entry in config file would be: PASS<WORD
I have used this site to find the correct pattern
What you have is valid:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
Are you trying to connect to an instanced sql server? Server=10.42.42.10/SomeInstance
I suggest adding the semicolon, ;, after the password in the connection string. Perhaps it's being parsed weirdly.
I also wonder whether: is a semicolon in your actual password?
Maybe you're attempting a Windows account but there's also a SQL account named the same way. If so, make sure you prefix the Windows account with the domain name. E.g. you might have Windows account of MYDOMAIN\sa with P#ssword, whilst SQL server account is sa and its password is P#ssw0rd.
To make sure what accounts exists, log in the SQL server as an admin and check what accounts are listed under "Security" node of both the server itself and the database of interest.
I had the same problem and here is my solution:
I found out that I was using named instance of an SQL server and changed Server of my connection string from Server=localhost to Server=localhost\SQLEXPRESS and everything worked just fine for me! :)

Connect a C# MVC application to a remote SQL Server database

I have a SQL Server database located at http://192.168.10.3/MyDB. I have created a C# MVC application, and I need to know the steps to connect my application to the above database.
Is it only replacing the connection string in the web.config file ?
Data Source=?? ;Initial Catalog=??;Integrated Security=SSPI;
User ID=??;Password=pwd;
If so what am I to replace where I have placed the ?? sign ?
DataSource = 192.168.10.3
Initial Catalog = MyDB
User ID = whatever sql login you are using to access your SQL Server
Password = password for the sql login above
The other answers here are good. In addition, ConnectionStrings.com can be your friend, especially if you are going to connect to various types of databases in the future. Select the database that you need to connect to and then you'll see the different connection strings you can use for that database.
http://connectionstrings.com/sql-server-2012#sqlconnection
you can try this
create a new text document on your desktop - conn.txt
change file extension to udl (conn.udl)
double click to open the file in the first tab select appropriate provider
4 . in the second tab enter server name (ip address,port), username, password (check Allow saving password) and database name.
test connection
if the test reports success close the window.
open the file with notepad, copy everything but the provider name and paste it back to connectionString
Below is connection string you need for:
Data Source="192.168.10.3" ;Initial Catalog=MyDb;Integrated Security=SSPI;
User ID=sa (for example);Password=whatever you set before;

How to resolve Azure "Windows logins are not supported in this version of SQL Server"?

I get the following error message, when I try to connect to SQL Azure.
Windows logins are not supported in this version of SQL Server
I'm using an Azure connection string. On development I'm running against SQL Server Express. This specific error is thrown when I try to fetch some data from the database.
The context that I'm using is running in a using clause, see below
function List<SomeType> GetList(string dbContextName)
{
using (MyDbContext context = new MyDbContext)
{
return context.SomeTypes.ToList();
}
}
We're using Entity Framework version 4.2, ASP.NET MVC 3 and .NET 4.0.
How can I resolve this issue?
I was using user/pass and still got the error message. But, I added this to my connection string and it worked.
Trusted_Connection=False;Encrypt=True;
Set
Integrated Security=False
In Connection String.
You've probably used the incorrect connection string, this is the connection string format that worked for my case:
"ConnectionString": "Server=tcp:xxxx.database.windows.net,1433;Database=xxx;User ID=xxx;Password=xxx;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Integrated authentication (i.e. SSPI in the connection string) is NOT supported in SQL Azure.
Only SQL Authentication is supported (i.e. username & password in the connection string)
As already mentioned by others, only SQL Server authentication is supported in SQL Azure.
You can read more on Guidelines and Limitations with SQL Azure. As well as the Security Guidelines and limitations for SQL Azure.
You have to CREATE LOGIN by yourself in your MASTER database, then you will want to CREATE USER in your custom Azure Database. Also do not forget to execute sys.sp_addrolemember to grant some permissions to your user.
More on managing users and logins in SQL Azure can be found here.
And, at the end, you can always look at the invaluable source for connection strings.
1.**Windows Authentication** is not supported in Azure so you should go with **SQL Server Authentication**.
2.When you use SQL server Authentication you should pass User Id(Login in SQL server) and Password(Password in SQL server).
3.User Id should contain space in between should not be like UserId.
4.Trusted_Connection should be false.
The connection string in *appsettings.json* look like this:
"ConnectionStrings": {
"DBContext": "Server=ServerName;Database=DbName;User Id=loginName;Password=loginPassword;Trusted_Connection=false;MultipleActiveResultSets=true"
}

INSERT statement doesn't work in WEB SERVER

IN localhost insert statement WORKS PERFECTLY ( insert data in database sql management server) but from web server it doesn't ( update, delete works but not insert).
I am using sql connection , string str = insert into dtbase.dbo.candidat values ().
command cmd = new command (sql, connection)
Can someone please tell me why it doesn;t work from wb server ( I am using web application.) do i need to add some permision in web.config?
To determine if this is a permissions issue (which I think it is) or not then temporarily (this is for the down voters out there) enable ASP.Net Impersonation by using an account that you know has access to your network and SQL Server instance: http://support.microsoft.com/kb/306158
Based on the other comments, I agree that it sounds like a permissions issue.
You may be getting the error using database.dbo.table because your table was created under a different schema (ie. database.user.table) and you're trying to access that schema from a user that doesn't have permissions to that schema.
Does your connection string change from localhost to your production server?

Categories