OleDB Connection String to MySQL Connection String - c#

I have this OleDB code which basically reads an excel file and displays it on to the datagridview after a button click:
string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(pathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("SELECT * FROM [" + txtSheet.Text + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dgvViewDrivers.DataSource = dt;
My question is that how would I make a MySQL Connection out of this OleDB connection string? Please help me.

You need to use MySQL .NET connector
http://dev.mysql.com/downloads/connector/net/
using (MySqlConnection conn = new MySqlConnection("SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"))
{
using (MySqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "sql here";
cmd.ExecuteNonQuery();
}
}
Your current code
This
using (vDBCon = new MySqlConnection("SERVER=localhost;Data Source=" + txtPath.Text + ";user=root;PASSWORD= ;"))
txtPath.Text needs to be the name of your database in MySQL. I assume you left the password out. If not you need one.
"SERVER=localhost;Data Source=MyDatabase;user=root;PASSWORD=MyPassword;"
where MyDatabase is the actual name of your database and MyPassword is the password you use to login with
You have to actually be running MySQL Server itself to create and connect to a MySQL database. it doesn't work of a file like Access. If you want something like that just use SQLite.
this
vCmd.CommandText = "SELECT * FROM [" + txtSheet.Text + "$]";
this you will be selecting data from your table in MySQL so it needs to look like
vCmd.CommandText = "SELECT * FROM MyTable";
where MyTable is actually the name of your table in the database

Related

I can't insert my data where I want in Excel

I can insert my data to Excel well, but I have a problem.
Here is a screenshot to illustrate.
I want to see my inserted data just below the last inserted data, but it shows up further down, in the screenshot as shown with a black circle.
Here is my code:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand("insert into [EVDS$] ([Tarih], [USD], [EUR]) values('" + DateTime.Now.ToShortDateString() + "','" + textBox3.Text + "','" + textBox4.Text + "')", connection);
command.ExecuteNonQuery();
connection.Close();

Excel update query putting all data in row 1

I'm using an oledbconnection to connect to an excel spreadsheet
Connection string:
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" +
Request.PhysicalApplicationPath + "tmp\\" + Session["Id"] + ".xlsx;" + "Extended Properties=\"Excel 8.0;HDR=NO;\"";
Update query:
cmdUpdate = new OleDbCommand("UPDATE [Contributions$B6:B6] SET F1 = 'value'", excelConn);
However, the data is being set in cell B1, not B6. All other queries are also adding the data in row 1, but the correct column...any ideas?

SELECT ODBC table and INSERT into SQL Server

I have an ODBC connection to a database of which I need one table of data with 10 columns.
I need to insert this table into SQL Server database. I´ve tried to many ways to do it but still with any results.
OdbcConnection OCon = new OdbcConnection("Dsn=" + DbSource.SelectedItem.ToString());
SqlConnection SCon = new SqlConnection("Data Source=" + SrvrDD.SelectedItem.ToString() + ";database=" + DdDestiny.SelectedItem.ToString() + ";User ID=id;Password=pass");
OCon.Open();
SCon.Open();
String QrySelect ="SELECT * FROM " + GridCon.Rows[x].Cells[1].Value;
OdbcCommand commandC = new OdbcCommand(QrySelect, OCon);
String QryInsert = "INSERT INTO " + DdDestiny.SelectedItem.ToString() + ".dbo." + GridCon.Rows[x].Cells[2].Value + " VALUES (#Sql)";
SqlCommand commandD = new SqlCommand(QryInsert, SCon);
OdbcDataReader Oreader = commandC.ExecuteReader();
commandD.Parameters.Add("#Sql",SqlDbType.NVarChar, 5);
while (Oreader.Read())
{
string s = Oreader[0].ToString();
commandD.Parameters["#Sql"].Value = s;
commandD.ExecuteNonQuery();
}
Everything works fine but when commandB.ExecuteNonQuery(); get in, an error appears:
"The column name or the specified values do not correspond to the definition of the table."
Is the translation.

Error SQL INSERT INTO with Odbc Command C#

Scenario:
I want to input data from textbox into the database based on microsoft data base (.mdb)
I already searching and find good clue and my result was here.
This Code below was inside command button click event:
using (OdbcConnection conn= new OdbcConnection())
{
conn.ConnectionString = #"Driver={Microsoft Access Driver (*.mdb)};" +
"Dbq=C:\\BlaBlaBla.mdb;Uid=Admin;Pwd=;";
conn.Open();
using (OdbcCommand cmd = new OdbcCommand(
"INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES ('" + txtFIELD1Input.Text + "','" + txtFIELD2Input.Text + "','" + txtFIELDInput.Text + "' )", conn))
{
cmd.ExecuteNonQuery();
}
conn.Close();
}
And when I click the command button, I get unfriendly exception
ERROR [42S02] [Microsoft][ODBC Microsoft Access Driver] Could not find
output table 'TABLENAME'.
That happened when I insert cmd.ExecuteNonQuery. If I didn't insert that, of course nothing happens in my table target.
So what mistakes did I make in that code? What should I do?
"INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES ('" + txtFIELD1Input.Text + "','" + txtFIELD2Input.Text + "','" + txtFIELDInput.Text + "' )", myConnection))
change this into
"INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES ('" + txtFIELD1Input.Text + "','" + txtFIELD2Input.Text + "','" + txtFIELDInput.Text + "' )", Conn))
you define Conn as your connection string not "myConnection"
So i changed to OleDbConnection And My Problem Cleared,
using (OleDbConnectionconn= new OleDbConnection())
{
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\LOSERONE\Documents\DATABASE\Latihan1.mdb";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand (
"INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES ('" + txtFIELD1Input.Text + "','" + txtFIELD2Input.Text + "','" + txtFIELDInput.Text + "' )", conn))
{
cmd.ExecuteNonQuery();
}
conn.Close();
}
Seems, to connected the database must same as the connection string in the properties on the targeted database.
Does anyone can tell me what is the difference OleDbConnection with OdbcConnection in .mdb database file?!
This problem is because sql connection's default database after login is not the same where your table 'TABLENAME' exists. Try to add database name before table like this:
INSERT INTO DBNAME..TABLENAME (FIELD1, FIELD2)
replace your myConnection to Conn

OleDbConnection s FillSchema doesn't change after alter table (Oracle)

I got a problem when using OleDbConnection in combination with an oracle Database.
When i request the schema of an existing Table using the OleDbConnection's FillSchema Method the result seems to get cached somehow.
To reproduce the Problem you need to do the followin steps:
create a table named cachetest with one column
Request a schema of the table cachetest in your code via FillSchema
Change the table by adding a column via alter table cachetest add column2 char(3)
request the schema of cachetest again. The Schema doesn't contain the column2
OleDbConnection connection = new OleDbConnection(
"Provider=" + "OraOLEDB.Oracle;"
+"Data Source=" + "datasource"
+ ";User Id=" + "msp" + ";"
+ "Password=" + "msp"
+";ChunkSize=1;" + "OLEDB.NET=" + "true;");
connection.Open();
DataTable dt = new DataTable("cachetest");
OleDbDataAdapter adapter_oledb =
new OleDbDataAdapter("select * from cachetest", connection);
adapter_oledb.FillSchema(dt, SchemaType.Source);
int columncount1 = dt.Columns.Count;
OleDbCommand command = new OleDbCommand("alter table cachetest add column2 char(30)", connection);
command.ExecuteNonQuery();
connection.Close();
OleDbConnection connection2 = new OleDbConnection(
"Provider=" + "OraOLEDB.Oracle;"
+"Data Source=" + "datasource"
+ ";User Id=" + "msp" + ";"
+ "Password=" + "msp"
+";ChunkSize=1;" + "OLEDB.NET=" + "true;");
DataTable dt2 = new DataTable("cachetest");
connection2.Open();
OleDbDataAdapter adapter_oledb2 = new OleDbDataAdapter(
"select * from cachetest", connection2);
adapter_oledb2.FillSchema(dt2, SchemaType.Source);
int columncount2 = dt2.Columns.Count;
This Problem doesn't appear using a SQL server...
Do you have any Idea how to solve this problem?
best regards martin

Categories