I can successfully log on to the database with this:
MySqlConnection conn = new MySqlConnection();
MySqlConnectionStringBuilder connString = new MySqlConnectionStringBuilder();
connString.Server = textEditServer.Text;
connString.UserID = "root";
connString.Password = textEditServerPassword.Text;
connString.Database = "geysercontrol";
conn.ConnectionString = connString.ConnectionString;
try
{
conn.Open();
Properties.Settings.Default.Properties["ConnectionString"].DefaultValue = conn.ConnectionString;
conn.Close();
}
catch (MySqlException)
{
XtraMessageBox.Show("No connection could be established");
}
But when I try to use the ConnectionString property to reconnect with different class, I get an MySQLException saying
Access denied for user 'root'#'localhost' (using password: NO)
What can be the possible causes to this? The page on possible causes on the MySQL website doesn't include my situation.
The code I use to reconnect is:
connection = new MySqlConnection();
connection.ConnectionString = (String)Properties.Settings.Default.Properties["ConnectionString"].DefaultValue;
connection.Open();
And the connectionString definitely is the same in both cases. It is:
server=localhost;User Id=root;database=geysercontrol;password=password
Add persist security info = true to the connection string I think.
If it were me though I wouldn't put connection string with a password in it in there. If you ever call Save, it will be exposed in the config file.
Related
I want to create a connection for my UWP and database. I want the uwp to send a value to the database.
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1; uid = root;" + "pwd=root;database=test";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
}
Is this the correct way to write the connection ? and where do I write this part of the coding at ?
enter image description here
Main page or any of my other page ? (scenario 1-3)
Your content looks right, I would try to use 'localhost' rather than ip.
var myConnection = new MySqlConnection();
myConnection.ConnectionString = "database=test;server=localhost;uid=root;pwd=root";
myConnection.Open();
More info see: https://dev.mysql.com/doc/dev/connector-net/6.10/html/P_MySql_Data_MySqlClient_MySqlConnection_ConnectionString.htm
I would also check here to see if you are providing enough info, which you are. https://www.connectionstrings.com/mysql/
In terms of connecting to the database, it depends. What type of application is this? Typically, database connections are made during the start of the application. If you are using Entity Framework, you'll want your Database Context to manage the connection (which is an entirely different topic).
I have a C# Windows app that I am creating with connection to a MS SQL server. I need the ability to switch to a local instance of SQL Server express if connectivity is down. This app is in a remote area where online is hit and miss. When connectivity goes down, I need the connection strings to reflect to the local server. I have logic to resync items from the local db to the live one once connection is restored. However, I am having issues trying to test connectivity automatically. The connection string in my app.config has both live and offline connections with the following code:
SqlConnection con = new SqlConnection(Properties.Settings.Default.MyLiveConnectionString);
If connection goes down, I want the local connection to use:
SqlConnection con = new SqlConnection(Properties.Settings.Default.MyLocalConnectionString);
Does anyone have a suggestion on how to make this happen seamlessly and quick?
I can test connections using try/catch, but it takes several seconds to time out before continuing.
public void Connect()
{
string connectionStr = string.Empty;
try
{
if(CheckConnection(Properties.Settings.Default.MyLiveConnectionString))
connectionStr = Properties.Settings.Default.MyLiveConnectionString;
}
catch (Exception ex)
{
//Log this, etc...
}
if (string.IsNullOrWhiteSpace(connectionStr))
connectionStr = Properties.Settings.Default.MyLocalConnectionString;
using (SqlConnection conn = new SqlConnection(connectionStr))
{
}
}
private static bool CheckConnection(string connectionString)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT 1", conn);
conn.Open();
if ((int)cmd.ExecuteScalar() == 1)
return true;
}
return false;
}
How can I connect to database using sql server authentication programmatically in c# winforms? I mean i created a login named loginTesting with a loginTesting as user in sql server 2012. I want to access that login in c# by accepting user inputs from textbox. Thanks.
You can use the class SqlConnectionStringBuilder to construct the connection string that you will need for a SqlConnection object:
SqlConnectionStringBuilder scsBuilder = new SqlConnectionStringBuilder();
scsBuilder.UserID = "username";
scsBuilder.Password = "password";
scsBuilder.DataSource = "servername or ip address";
scsBuilder.InitialCatalog = "databasename";
scsBuilder.IntegratedSecurity = false;
using (SqlConnection sqlCon = new SqlConnection(scsBuilder.ConnectionString))
{
// perform your SQL tasks
}
Further reading here:
SqlConnectionStringBuilder Class
SqlConnection Class
Expanding on Answer 1, if you'd like to test your usernames/passwords based on user input you could use the SqlConnectionStringBuilder object as shown in this example:
SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
connectionString.ConnectionString = #"data source=.;initial catalog=master;";
connectionString.UserID = "username";
connectionString.Password = "password";
using (var sqlConnection = new SqlConnection(connectionString.ConnectionString))
{
}
You need to create a new SQLConnection (and clear it up once you've used it).
using(var connection = new SqlConnection(connectionString))
{
// Do something
}
The tricky bit is getting the correct connection string, but fortunately there are resources to help. Checkout ConnectionStrings.com to find a connection string that looks like it should work, plug in your credentials/server etc and you're good to go.
I created a database (without user or password) as a service-based database.
Now I'm trying to insert to a database, but there is a error "Login failed for user"
I have this code:
string J_connetionString1 = null;
SqlConnection J_connection1;
SqlDataAdapter J_adapter1 = new SqlDataAdapter();
string J_sql1 = null;
J_connetionString1 = #"Data Source=.\SQLEXPRESS;Initial Catalog=J_InsData";
J_connection1 = new SqlConnection(J_connetionString1);
J_sql1 = "update instinfo set instinfoNAME = textBox2.text where instinfoID ='1'";
try
{
J_connection1.Open();
J_adapter1.UpdateCommand = J_connection1.CreateCommand();
J_adapter1.UpdateCommand.CommandText = J_sql1;
J_adapter1.UpdateCommand.ExecuteNonQuery();
MessageBox.Show("Row updated !! ");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
i can't comment so i edited the Q.
this error appeared by using this code
error :
system.Data.sqlclint.sqlexpection(0x80131904)
cannot open database "J_InsData" request by the login. the login faild
login failed for user J-PC\J
code:
J_connetionString1 = #"Data Source=.\SQLEXPRESS;Initial Catalog=J_InsData;Trusted_Connection=True;Integrated Security=SSPI";
You need to tell SQL Server which user credentials to use for a connection.
Hence you should provide a user id and password of SQL Server user in your connection string.
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
On the other hand if the SQL erver is configured to accept windows domain users, you can use Integrated Security=SSPI or Trusted_Connection=True in the connection string.
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Server=myServerAddress;Database=myDataBase;Integrated Security=SSPI;
Have you tried adding the following to your connection string?
Integrated Security=True
or
Integrated Security=SSPI
which is pretty mcuh the same as True.
This LINK might be useful for you as well.
Please tell how to validate dataSource name and PortNumber in Connection String of SqlConnection. Connection state changes to Open, even I donot give any value for dataSource. Like the code below..
var Connection = new SqlConnection("Data Source=;Trusted_Connection=True");
try
{
Connection.Open();
MessageBox.Show("Connection Succeeded");
}
My requirement is I need to validate the Data Source name and Port Number that are entered by EndUser.
For offline validation, use SqlConnectionStringBuilder to parse it...
SqlConnectionStringBuilder myconBuilder = new SqlConnectionStringBuilder();
myconBuilder.ConnectionString = "Data Source=;Trusted_Connection=True"; //Throws exception if garbage connection string like 'abcd' is supplied
if (string.IsNullOrWhiteSpace(myconBuilder.DataSource))
{
//Throw exception that data source specified is blank
}