C# SQL Server connection using server Login - c#

I have a problem with connecting to my SQL server. I keep getting "Login failed for user" error messages, but the user I am trying to login as exists as a login on the server. The connection string I use looks like this:
Data Source=[server name];Initial Catalog=[database];User ID=[login name];Password=[login password];
Is it because the login exists on the server, but not as a user on the database that I can't use it to login?
If so is it not possible for me to connect to the server using a server login?
Thanks in advance.

It may be that there is something else wrong with your connection string. I know if I have the wrong database or server in the string it will be give me an "unable to login with user..." message.
Data Source=[server name];Initial Catalog=[database];User ID=[login name];Password=[login password];

Use this code to connect SQL Server (preferrd Language is C#)
Public Sub Start_DB_Connection(servername As String,user As String,password As String,dbname As String)
Try
db_con As New SqlConnection
If Not db_con Is Nothing Then db_con.Close()
db_con.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; connection timeout=1;", servername, user, password, dbname)
db_con.Open()
Catch ex As Exception
// show error
End Try
End Sub

Related

Querying a website database from a C# console application

I'm trying to write a simple app in C# that will query a database available to the public located at https://newswire.theunderminejournal.com/
They give an example located at https://newswire.theunderminejournal.com/sample2.php
As far as I know, the server I'm trying to connect to is "newswire.theunderminejournal.com" and the database is "newsstand".
This gives me a string of:
string connection_str ="Server=newsstand.theunderminejournal.com;Database=newsstand";
My code looks like this:
xData = "Unable to connect to database.";
string server = "'newswire.theunderminejournal.com'";
//string server = "45.75.164.122";
string database = "'newsstand'";
string connection_str = string.Format("Server={0};Database={1};", server, database);
Console.WriteLine(connection_str);
SqlConnection connection = new SqlConnection(connection_str);
try
{
connection.Open();
xData = "Connection established!";
}
catch(Exception e)
{
xData = e.Message;
}
connection.Close();
connection.Dispose();
This results only in an error message:
"A network-related or instance-specific error occured while not
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:
Named Pipes Provider, error: 40 - Could not open a connection to SQL
Server)"
I've pinged the server, getting an IP address at 45.79.164.122 and no issues at all pinging it.
Is there something obvious I'm missing here? I'm brand new to C# so if there is another/better way of doing this, I'd appreciate it!
Thanks.
You cannot create a SqlConnection to a mySQL database, which is why the error message refers to "… establishing a connection to SQL Server.".
You need to use a MySqlConnection instead, which you get when you install Connector/Net - which you can download from Oracle here

MySql access denied for user - can't create the user the software is looking for

My connection string looks like this:
string server = "localhost";
string database = "stman";
string user = "logan";
string pass = "root";
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", server, database, user, pass);
Given my understanding, this means MySql should be using 'logan'#'localhost' for the login, but it isn't.
It looks as though it's using 'logan'#<server's fully qualified name>:
Access denied for user 'logan'#'HP-PL-ML110.young.home'
I'm at a loss right now. SqlYog doesn't allow me to create a user with that hostname, my options for hostname are %, localhost or 127.0.0.1
Can anyone help me make sure that the website is using the specified username here? I have no idea what to look at to fix this
Joe's comment:
Have you tried disabling host name lookup, as in this question
Applying this didn't fix the issue but it did change the settings so that I could use the IP address in the connection string.
This does mean I had to create a new user (I found that, in Sqlyog, you can enter something different in the hostname field in user creation) and grant the new user the required permissions on the target database.
It's not exactly a solution, but it is a viable workaround that's got me back to a point where I can actually progress further in my development now.
Thanks Joe
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={4};", server, database, user, pass);
Should be
string connStr = String.Format("Server={0};Database={1};Uid={2};Pwd={4};", server, database, user, pass);

Error connecting to SQL server through string, not recognising user name or password

the problem I am having is connecting to an account on my sql server (2005) from an ASP.NET application.
Ive tried using the default sa login and users ive created already also the setting of the sql management studio are set to mixed properties, I have the string connection in the webconfig as well but also doesnt work.
c# code
//string conStr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
string conStr = #"server=JAMES-PC\SQLEXPRESS; database=projectDB; uid=james; password=password;";
string query = "SELECT [TaskID], [Task], [Start Date] AS Start_Date, [End Date] AS End_Date, [Priority], [Time Allowance] AS Time_Allowance, [Details], [Catagory] FROM [schedulerData0]";
SqlDataAdapter dataAdapt = new SqlDataAdapter(query, conStr);
DataTable table = new DataTable();
dataAdapt.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
The error message I receive is:
Login failed for user 'james'. The user is not associated with a trusted SQL Server connection.
Any help appreciated
James
Your SQL SERVER configured for Windows Only connections and you current windows user not associated as trusted. Try to configure your SQL SEREVR to accept Mixed Mode connections.
Try this,I'm not sure but hope it will work-
<connectionStrings>
<add name ="conStr" connectionString ="Initial Catalog = projectDB;
Data Source =JAMES-PC\SQLEXPRESS; User Id=james;Password=password;"/>
</connectionStrings>
try mapping projectDB to user:james. open SQL Server Management Studio, select Security - Logins, double click user:james, select page:User Mapping, check projectDB.
Please try the following format If It is Sql Server user mode,
ConStr = "Server=JAMES-PC\SQLEXPRESS;Database=projectDB;User Id=james;
Password=password;"
if you are trying to connect using windows,
then you must provide Trusted Connection = true

SQLConnection connection error "Login failed for user server/guest"

I keep receiving a connection error. I know that the information is right, as I tried them out with SSMS now several times in the last few minutes, so the problem is in the C# connection string. According to various Google searches, the syntax is right, just it does not work.
try
{
// Get the connection information.
GetSQLConnectInfo(ref sConnect);
// Formulate the connection string.
String strConnect = String.Format("Server=myserver;Database=mydb;User Id=myusername;Password=mypassword;Trusted_Connection=yes;connection timeout=30");
// DATABASE: Create the connection.
SqlConnection oConnection = new SqlConnection(strConnect);
oConnection.Open();
if (ConnectionState.Open != oConnection.State)
return false;
return true;
}
catch
{
}
Error comes back with: Login failed for user 'myserver\Guest'.
Error Code: -2146232060
Error Number: 18456
It would seem that judging by the error message, the Open method does not recognize the user name, as Open tries to use a guest account, which obviusly will not work.
Am I missing something? I am using SQL Server authentication.
Remove Trusted_Connection=yes. That will override your SQL Authentication (username/password) settings and try to log in as the user running the app.

Connect to SQL Server CE database with password

How can I connect to the .sdf database with a password and still be able to use the tableadapter to FILL out my listbox with this code:
try
{
tbl_sClipManagerTableAdapter1.Fill(db_sClipManagerDataSet1.tbl_sClipManager);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Produces this error:
The specified password does not match the database password. [ Data Source = PATH TO .SDF FILE ]
I have password protected the database. How can I connect with password and still through the tableadapter?
EDIT:
Oh, I don't want to store the password with the connection string - cause it is to easy readable through the config file.
SOLUTION:
I found the solution by myself.
In DataSet.Designer.cs find something like:
private void InitConnection() {
this._connection = new global::System.Data.SqlServerCe.SqlCeConnection();
this._connection.ConnectionString = global::sClipManager.Properties.Settings.Default.db_sClipManagerConnectionString + "; password=PASSWORDHERE";
I just added the + "; password=PASSWORDHERE".
Everything works now. And the password can't be easily read in the config file.
Best regards
I'm hoping that query is being executed by some kind of soap/rest service. If so, you can have that service run with domain credentials. Then you could give that domain-login access to the db. Then you could use integrated security in the connection string. That way the connection string has -zero- credential info.

Categories