My connection to my database is not working. It tries to load for a long time then says the connection failed. All i want to do is access the database and print out a name. I am brand new to this but here is what i have so far:
string connectionstring = "uid=user;server=it.et.byu.edu;port=xxxxx;database=database;password=password;";
MySqlConnection connection = new MySqlConnection(connectionstring);
connection.Open();
MySqlCommand cmd = new MySqlCommand("SELECT username FROM Users WHERE username='george'", connection);
try
{
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(0));
}
rdr.Close();
}
connection.Close();
Check your connection string and make sure you can connect/manipulate your MYSQL database if you are using XAMPP or other Mysql Tools using your credentials(uid,password).
For introduction this might help you :
ADO.NET With MySQL and MSDE
Connection strings for MySQL
Best Regards
Related
So i was trying to connect a c# gui app to a xampp sql server here's the code:
try
{
string connectionString = "Server = localhost:8080; database = blog; Uid=root;Pwd";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into logins(username,password) values('name','password')", conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this is the code username is root and no password
but when the program starts it raises an exception saying:
Format of the initialization string does not conform to specification starting at index 52.
Whats causing this problem and how can i solve it?
Thanks in advance
SQLConnection class is used to connect to sqlserver and to Connect Mysql MySqlConnection will be used
you need to add MySql Library in your C# project.
using MySql.Data.MySqlClient;
Config
string myConnectionString = "server=localhost;database=testDB;uid=root;pwd=pwd;";
Use
string connetionString =""; //get from config;
MySqlConnection cnn ;
cnn = new MySqlConnection(connetionString);
I have added a SQL Server .mdf database file to my C# application, but when I try to connect with this code, the program causes a connection error.
CODE:
DataSet data;
string con = "Data Source=dbinterno.mdf;";
string queryString = "Select * FROM Dati";
try
{
using (SqlConnection connection = new SqlConnection(con))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
data = new DataSet();
adapter.Fill(data);
MessageBox.Show(data.ToString());
connection.Close();
}
}
catch
{
MessageBox.Show("\n Problemi di connessione al database");
}
The error is:
ERROR IMAGE
Here are a couple observations:
Your connection string will need to be modified. Try using
string con = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
using Windows Authentication or this:
string con = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;"; using standard security, Source: connectionstrings.com. This should be managed some other way than in code as well. Desktop applications can be de-compiled, and if the password changes, you would need a rebuild. In a ASP.NET application, Microsoft advises to use a web.config file or in the windows registry using a custom subkey.
You will want to use ExecuteReader() for a SELECT statement as ExecuteNonQuery() will not return a result set. See this answer that describes the differences in the types of SQL Server methods
you don't need connection.Close();, the using statement will handle that.
Is there anything wrong with this code? Please help me out.
protected void Button_Click(object sender, EventArgs e)
{
string cs = "Data Source=SFSIND0402;Initial Catalog=TestDB;Integrated Security=SSPI;Provider=Microsoft.ACE.OLEDB.12.0";
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand insert = conn.CreateCommand();
insert.CommandText="insert into Employee(ID, Name, Sex, Salary) values('003','Vedpathi','M',25000)";
insert.Connection = conn;
insert.ExecuteNonQuery();
conn.Close();
}
I am getting the following error:
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done
(on line 22:conn.Open();)
When connecting to an MS SQL database, use the MS SQL providers:
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var cmd = new SqlCommand(commandText, connection);
cmd.ExecuteNonQuery();
}
In addition to the solution Luaan mentioned, you should store your connection string in the config file of the app and also encrypt it.
Even if you use SSL encryption when communicating with the DB, an ill-indended person can extract the string variables, if he / she runs the application on his / her machine.
I try to connect to a MySQL database on a server a friend of mine has created, but it still says it cannot connect to the server!
Here is my code:
String connectionString = "Persist Security Info=False;database=qwertyui;server=www.qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;Connect Timeout=30";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO etc.... ");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue( etc.... );
connection.Open();
int r = cmd.ExecuteNonQuery();
}
It failes on the connection.Open() instruction throwing an exception -> cannot connect to the server.
Any ideas please?
SqlConnection is a SQL Server client.
You need to download a MySql client from NuGet.
First download the MySQL Connector.
Make sure you add a reference to the DLL (+ set copy local to true !)
Add using MySql.Data.MySqlClient; on top of your class.
As for the Connection String, do not add http or www in front of server. As it will try to connect to apache (port 80) instead to MySQL. The default port of MySQL is 3306.
string connectionString = "Persist Security Info=False;database=qwertyui;server=qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;port=3306";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
string query = "INSERT INTO table VALUES (#parameter)";
connection.Open();
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("#parameter", parameter);
cmd.ExecuteNonQuery();
}
}
If this is not working. Make sure MySQL allows remote access, if this is not the case, you can keep trying forever without any result.
First thing that comes to mind is that you will have to use a MysqlConnection and command.
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html
Is it a local database?
And if with all of above does not work try to get grant to all privileges(GRANT Syntax)!
Basically it will go something like:
GRANT ALL PRIVILEGES ON DBname.* to 'username'#'IPadress' IDENTIFIED BY 'password';
I can't find the mistake in my code below.When I use SQLDataSource to connect my database,there's no error.However,if I try to write the custom code to connect the database using the same connection string SQLDataSource uses,I encounter this error:
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I suppose there's no mistake in my code,but in the configuration settings of SQL 2008 Express.
Thanks in advance...
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU/SQLEXPRESS;Initial Catalog=KET;Integrated Security=True";
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection=sqlcon;
sqlcmd.CommandText = "SELECT * FROM Login";
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
Shouldn't this read:
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU\\SQLEXPRESS;Initial Catalog=KET;Integrated Security=True";
There problem is in the statement in sqlcmd.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Login", sqlcmd))
DataTable t = new DataTable();
adapter.Fill(t);
Actualy you are executing a query and you need either SqlDataReader or SqlDataAdapter here.
Try to check if your SQL server allows remote connections, you can set that in the sql server configuration manager
and then enable the remote TCP connection
You should change your line from:
sqlcmd.ExecuteNonQuery();
to:
SqlDataReader dr = sqlcmd.ExecuteReader();
If you are using Windows Authentication on your SQL database you may need to change the Integrated Security to SSPI? Although I'm not overly sure. - edit reading some information about this and SSPI is equivalent to True, so see suggestions below instead.
sqlcon.ConnectionString = "Data Source=ERHANEMREEROGLU/SQLEXPRESS;Initial Catalog=KET;Integrated Security=SSPI";
But as John Blade said you may also need to check in the Configuration Manager that it accepts remote connections.
Also make sure you've added the Windows user to the database. You can do this by using the SQL Mangement Studio Tools.
You are executing ExecuteNonQuery(); which is for Insert/update/delete statements. You may fill a DataReader using ExecuteReader or a dataset/datatable using SQLDataAdapter.
SqlDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
You can try:
using (SqlConnection sqlcon = new SqlConnection(
"Data Source=ERHANEMREEROGLU\\SQLEXPRESS;Initial Catalog=KET;Integrated Security=True"))
{
sqlcon.Open();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Login", sqlcon ))
{
DataTable t = new DataTable();
a.Fill(t);
}
}