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