I have xampp installed in my computer. I am trying to access data with ADO.Net. The connection string I am using is given below:
string connectionString = "Server = localhost; Database = magento; User Id = magento; Password = abcd;";
SqlConnection con = new SqlConnection(connectionString);
string cmdString = "SELECT date_added,title,description,url FROM adminnotification_inbox";
SqlDataAdapter da = new SqlDataAdapter(cmdString, con);
ds = new DataSet();
da.Fill(ds,"prog");
dt = ds.Tables["prog"];
currRec = -1;
totalRec = dt.Rows.Count;
button3.Enabled = true;
I am able to log in with the above user id and password in phpmyadmin, but cannot access the database with the above connection string. please help. Thanks in advance.
MySQL has its own ADO.NET connector: http://dev.mysql.com/downloads/connector/net/6.6.html#downloads
If you use that, you can create a MySqlConnection: http://dev.mysql.com/doc/refman/5.5/en/connector-net-tutorials-intro.html
The basic SqlConnection is used for Microsoft's own SQL Server products.
9-22-14 - Hope others see this if you don't:
You need a driver in your connect string I believe. "MySQL ODBC 3.51 Driver" is the Window's driver name.
string connectionString ="Driver={MySQL ODBC 3.51 Driver}; SERVER= .... ok put the rest of your connect string here.
Note: this is the string to connect to a MySQL db using MS Access VBA:
Dan
Related
Is self signed certificate disrupting connection to mysql?
I already give action crud in data and grant access to administration.
but the connection still got error.
Unable to connect to any of the specified MySQL hosts
my team connection like this:
Catalog=devbaf;User Id=userclient;password=client123
connection code:
try
{
string constring = "SERVER=123.45.678.9;DATABASE=devbaf;User Id=backendsystem;PASSWORD=1q2w3e4r5t;";
using (MySqlConnection con = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM users"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvtest.DataSource = dt;
gvtest.DataBind();
}
}
}
}
}
catch (Exception e)
{
Response.Write(e.Message.ToString());
}
explanation problem:
I make a database in local server, so my friend can access to it to insert data. but when he make a connection script it wont connect to my database but can connect to server. He already tries to move some file to the server and it works,but the connection to database wont connect. i don't have an experience for this coz im php programmer not c# programmer. hope my explanation is clear.
I have found your error message like trying to connect the host but in your connection string is missing the MySQL server name or host address. I hope if you add the MySQL hostname or server address to your connection. it will fix your issue.
you can try this
string connectionString ="SERVER=servername; DATABASE=databasename; User Id=userid; PASSWORD=password;";
I'm trying to connect to MS SQL using ODBC but keep getting the
"Login failed for user 'User-PC\User'" error.
web.config
<add name="SQLDbConnection" connectionString="Server=127.0.0.1; Database=HMS; Integrated Security=false; User Id=sa; password=root" providerName="System.Data.Odbc "/>
C#
string query = "...";
OdbcConnection msSQLConnection = new OdbcConnection(strConnection);
OdbcCommand command = new OdbcCommand(query, msSQLConnection);
command.Connection.Open();
I tried using the below and it's ok. Any idea how I can get ODBC to work?
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString()))
{
SqlCommand cmd = new SqlCommand("SELECT COMPANY_ID from COMPANY", cn);
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
rdr.Read();
}
SQL Connection and ODBC Connection do not use the same connection strings. For the ODBC connection you need to specify the driver.
For SQL Server 2012:
Driver={SQL Server Native Client 11.0};Server=127.0.0.1;Database=HMS;Uid=sa;Pwd=root;
You should use something like this :
DRIVER={MySQL ODBC 3.51 Driver}; SERVER=127.0.0.1; DATABASE=HMS; USER=sa; PASSWORD=root;
Then take a look in here :OBDC example
and then in here : Connection strings
I'm connecting my windows form application to SQL server but I get this error "SqlException was unhandled" when I start the program.
Here is my code:
void ShowEmployees()
{
using (SqlConnection Connect = new SqlConnection("Data Source=(local);" + "Database='SanMarDryCleaners';" + "Integrated Security=SSPI;"))
{
string strEmployees = "SELECT * FROM employees;";
SqlCommand cmdEmployees = new SqlCommand(strEmployees, Connect);
SqlDataAdapter daEmployees = new SqlDataAdapter();
daEmployees.SelectCommand = cmdEmployees;
DataSet dsEmployees = new DataSet("EmployeesSet");
daEmployees.Fill(dsEmployees);
Connect.Open();
dgvEmployees.DataSource = dsEmployees;
dgvEmployees.DataMember = dsEmployees.Tables[0].TableName;
}
}
Here is the screenshot:
The error suggest that it can't find the SQL Server. And that makes sense because you are using a SqlConnection class which is for Microsoft SQL Server only. You have to use MySQL libary.
Download it from this site:
http://dev.mysql.com/downloads/connector/net/
The error points to a problem while connecting to the server.
A SqlConnection is for SQL-Servers. Use a library specific to MySQL and a MySqlConnection.
See:
Connector/Net Installation
MySQL Connector/Net connection strings
I tried connecting using a connection string and get this error:
"Format of the initialization string does not conform to specification starting at index 0."
here is my code :
SqlConnection Con = new SqlConnection(#"C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\TARGIL3.MDF");
SqlDataAdapter adapt = new SqlDataAdapter();
adapt.InsertCommand = new SqlCommand(" INSERT INTO tblEmployee VALUES (#employeeNumber, #employeePrivateName, #employeeFamilyName ,#city, #street, #houseNo, #phoneNumber, #birthDate, #startWorkingDate)", Con);
adapt.InsertCommand.Parameters.Add("#employeeNumber", SqlDbType.Char).Value = textBox1.Text;
adapt.InsertCommand.Parameters.Add("#employeePrivateName", SqlDbType.VarChar).Value = textBox2.Text;
adapt.InsertCommand.Parameters.Add("#employeeFamilyName", SqlDbType.VarChar).Value = textBox3.Text;
adapt.InsertCommand.Parameters.Add("#city", SqlDbType.VarChar).Value = textBox4.Text;
adapt.InsertCommand.Parameters.Add("#street", SqlDbType.VarChar).Value = textBox5.Text;
adapt.InsertCommand.Parameters.Add("#houseNo", SqlDbType.Int).Value = textBox6.Text;
adapt.InsertCommand.Parameters.Add("#phoneNumber", SqlDbType.Char).Value = textBox7.Text;
adapt.InsertCommand.Parameters.Add("#birthDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
adapt.InsertCommand.Parameters.Add("#startWorkingDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
Con.Open();
adapt.InsertCommand.ExecuteNonQuery();
Con.Close();
use this:
string connString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME; User ID = USERNAME; Password=PASSWORD;Integrated Security=false";
SqlConnection Con = new SqlConnection(connString);
You connection string is wrong. It should be of this form if you use SQL Server Authentication:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
Where myServerAddress is the IP or host name of the computer running SQL Server. If it's your PC, localhost will probably work.
myUsername and myPassword are the credentials of the user connecting to the server.
If you use Trusted Connection,you connection string should be of this form:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
You can check more examples here: http://www.connectionstrings.com/sql-server/
You cannot just put a file name in the connection string
C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\TARGIL3.MDF
Sql Server is a usually a "service", and you talk to the service..........
Now. If you are using "Express" version......then you may have to include the AttachDbFilename parameter.
http://www.connectionstrings.com/sqlconnection/attach-a-database-file-on-connect-to-a-local-sql-server-express-instance-1/
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;
But if you are NOT using "Express", then you should only put info about the Sql Server SERVICE (machine\instancename, etc, etc), and not care about the physical path of the .MDF.
Sql Server is not JET (Access). With JET, you would only specify the filename.
If you're still having issues, you should post which version of sql server you are using.....do a "select ##version" and report that.
I'm trying to connect to the database, and it keeps telling me "invalid instance".
Here's my code:
string connectionString = "Driver={SQL Server};Server=server;Database=db;;Uid=user;Pwd=pass;";
OdbcConnection MyConnection = new OdbcConnection();
MyConnection.ConnectionString = connectionString;
MyConnection.Open();
What is the problem?
Thanks!
Try using this.
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
And if you are using MS Sql server, better use with SqlConnection.
using(SqlConnection conn = new SqlConnection(connestionString)){
conn.open();
..
}
http://www.connectionstrings.com/sql-server-2005