Excel update query putting all data in row 1 - c#

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?

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();

OleDb ignores the texts starting with 0

I'm using OleDbDataAdapter
I'm trying to get data from excel to DataTable, Here is my code;
var conn = new OleDbConnection(strConn);
var myCommand = new OleDbDataAdapter(" SELECT * FROM [UPLOADFILE$] ", strConn);
var inputTable = new DataTable();
try
{
myCommand.Fill(inputTable);
}
Here is my Conn Str;
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
Somehow, It eliminates the strings starting with "0", but others are okay.
I used the following combinations, as well;
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;IMEX=0;TypeGuessRows=0;ImportMixedTypes=Text\"";
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "MyPath" + ";" +
"Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
Thanks in advance!
My Tries;
Excel 8.0;HDR=No;IMEX=1;
For what it's worth, this is my go-to connection string for reading excel via OleDb.
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'; ";
For what it's worth I tried it on some data and had no issues when I had text with leading zeroes.
Is your data in a table or is it just a standard range? Does it have headers? Can you show some sample data that is erroring out?

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.

OleDB Connection String to MySQL Connection String

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

Cant create Excel file using OLEDB C#

string TempFileLocation="Filelocation";
string tempfilename ="FileName";
string TabName ="TabName";
string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +TempFileLocation+ tempfilename +".xls;Extended Properties='Excel 8.0;HDR=YES'";
var conn = new OleDbConnection(xConnStr);
string ColumnName ="[columename] varchar(255)"
conn.Open();
var cmd = new OleDbCommand("CREATE TABLE [" + TabName + "] (" + ColumnName + ")", conn);
cmd.ExecuteNonQuery();
conn.Close();
I using above code to create the table but it did not allow me to create ColumnName with more than 64 characters. Please give me soluition for this problem.
The column name cannot be over 64 characters.
According to MSDN:
Maximum column name length:
Column names over 64 characters will produce an error.

Categories