Dynamically insert into SQLite from text file - c#

I am trying to create dynamical insert statements from text file, with only SQLite.
What I have done so far, is to create the SQL query with necessary parameters, add those parameters during run time, and try to select.
However I get error inside try block, when try to cmd.ExecuteNonQuery();
Caught exception: SQL logic error or missing database
near "#0": syntax error
using System;
using System.Data.SQLite;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
namespace TestApp
class Program
{
static void Main(string[] args)
{
//To store the index and column name in the file
Dictionary<int, string> Columns = new Dictionary<int, string>();
char[] delimiterChars = { '\t' };
string createQuery = #" create table if not exists products(id integer not null primary key, name text);
insert into products (name) values ('A');
insert into products (name) values ('B');
insert into products (name) values ('C');
insert into products (name) values ('D');
insert into products (name) values ('E');
insert into products (name) values ('F');
insert into products (name) values ('G');
create table if not exists orders(id integer, dt datetime, product_id integer, amount real);";
System.Data.SQLite.SQLiteConnection.CreateFile("myDB.db3");
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=myDB.db3")){
using (System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn)){
conn.Open();
cmd.CommandText = createQuery;
cmd.ExecuteNonQuery();
string[] lines = System.IO.File.ReadAllLines(Directory.GetCurrentDirectory() + #"../../../App_Data/import.txt");
cmd.CommandText = "INSERT INTO orders (";
// Identify the column order from first row of the import file
string[] elements = lines[0].Split(delimiterChars);
for (int i = 0; i < elements.Length; i++)
{
Columns[i] = elements[i];
cmd.CommandText = cmd.CommandText + "#COLUMN" + i + ", ";
cmd.Parameters.AddWithValue("#COLUMN" + i, Columns[i]);
System.Console.WriteLine(i + " : " + Columns[i]);
}
cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.Length - 2);
cmd.CommandText = cmd.CommandText + ") VALUES (";
string temp = cmd.CommandText;
System.Console.WriteLine(cmd.CommandText);
System.Console.WriteLine("Contents of Import File.txt = ");
for (int i = 1; i < lines.Length; i++)
{
cmd.CommandText = temp;
elements = lines[i].Split(delimiterChars);
for (int j = 0; j < elements.Length; j++)
{
cmd.CommandText = cmd.CommandText + "#VALUE" + j + ", ";
cmd.Parameters.AddWithValue("#VALUE" + j, elements[j]);
}
cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.Length - 2);
cmd.CommandText = cmd.CommandText + ")";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Caught exception: " + ex.Message);
}
Console.WriteLine(cmd.CommandText);
Console.WriteLine(lines[i]);
}
cmd.CommandText = "Select * from orders";
cmd.ExecuteNonQuery();
using (System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader()){
while (reader.Read())
{
Console.WriteLine(reader["ID"] + " | " + reader["dt"] + " | " + reader["product_id"] + " | " + reader["amount"]);
}
conn.Close();
}
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
}
I am not sure on what am I doing wrong?
The import.txt file consists of
id dt amount product_id
1 2017-02-01T10:01:12 343.33 1
2 2017-02-01T10:02:12 12 2
3 2017-03-01T10:03:12 344.3 1
4 2017-04-01T10:04:12 12 3
5 2017-05-01T10:05:12 66.5 1
6 2017-06-01T10:06:12 4
All items divided by TAB

The loop over the column names is useless because you already know the column names from the CREATE TABLE ORDERS command executed at the first lines.
By the way, you cannot use parameters to express the name of a column.
This is not allowed in any kind of database system that I know of.
You can safely remove it but note that you have declared the column order wrongly. In the CREATE TABLE you have
create table if not exists orders(
id integer, dt datetime, product_id integer, amount real
while in the file the product_id is the last column. So you need to adapt your CREATE TABLE to your file
create table if not exists orders(
id integer, dt datetime, amount real, product_id integer;
Next your insertion loop code could be rewritten in this way (ignoring the variable number of arguments as you explain)
string baseQuery = "INSERT INTO orders (id, dt, amount, product_id ) VALUES(";
string[] lines = System.IO.File.ReadAllLines(#"e:\temp\orders.txt");
// Skip the first line
for (int i = 1; i < lines.Length; i++)
{
string[] elements = lines[i].Split(delimiterChars);
// Keep the parameter names in a list to get an easy way to
// concatenate them all together at the end of the loop
List<string> text = new List<string>();
for (int j = 0; j < elements.Length; j++)
{
text.Add("#VALUE" + j);
cmd.Parameters.AddWithValue("#VALUE" + j, elements[j]);
}
// Create the command text in a single shot
cmd.CommandText = baseQuery + string.Join(",", text) + ")";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Caught exception: " + ex.Message);
}
}
Consider also to enclose your database code inside a transaction like this as suggested by the link posted below by Alexander Petrov
using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=myDB.db3"))
using (System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
using(SQLiteTransaction tr = conn.BeginTransaction())
{
cmd.Transaction = tr;
.....
for(....)
{
.....
}
tr.Commit();
}
}

Related

Inserting decimal numbers from DataGridView into MySQL using INSERT

everybody,
I want to insert data from a DateGridView table into MySQL. It works so far but as soon as there are decimal numbers then they are not stored as decimal in MySQL but as integers. The decimal places are not transferred. I tried to convert the data but without success. They are not stored as decimal numbers.
Thanks for the tips !!!!
Here is the code
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) // dòng
{
string str = "server=test; database=test; uid=test; pwd=test;";
MySqlConnection constr = new MySqlConnection(str);
constr.Open();
String cmdText = "INSERT INTO KPI_Kosten (Betrag, Tower, Jahr, Periode, Land) VALUES ('"
+ Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value) + "','"
+ dataGridView1.Rows[i].Cells[1].Value + "','"
+ dataGridView1.Rows[i].Cells[2].Value + "','"
+ dataGridView1.Rows[i].Cells[3].Value + "','"
+ dataGridView1.Rows[i].Cells[4].Value + "' )";
MySqlCommand cmd = new MySqlCommand(cmdText, constr);
cmd.ExecuteNonQuery();
constr.Close();
}
}
Since decimal places are truncated because column data type definition set to integer, first you should modify Betrag column to decimal data type with certain precision and scale like example below:
ALTER TABLE KPI_Kosten MODIFY COLUMN Betrag decimal(10, 2);
Afterwards, try setting MySqlParameter with parameterized query to enforce decimal data type with specified settings (see documentation):
string str = "server=test; database=test; uid=test; pwd=test;";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
using (MySqlConnection constr = new MySqlConnection(str))
{
constr.Open();
// if '#' prefix for parameters not working, change to '?'
String cmdText = "INSERT INTO KPI_Kosten (Betrag, Tower, Jahr, Periode, Land) VALUES (#Betrag, #Tower, #Jahr, #Periode, #Land)";
using (MySqlCommand cmd = new MySqlCommand(cmdText, constr))
{
// if you're experiencing too many parameters error, uncomment this line below
// cmd.Parameters.Clear();
MySqlParameter betrag = new MySqlParameter("#Betrag", MySqlDbType.Decimal);
betrag.Precision = 10;
betrag.Scale = 2;
betrag.Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.Add(betrag);
cmd.Parameters.Add("#Tower", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[1].Value;
// other parameters
cmd.ExecuteNonQuery();
}
}
}
Note: You can also try simplified version without precision like cmd.Parameters.Add("#Betrag", MySqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);.

Copying tables among databases

Let's say I want to copy all tables with their complete data from one database to another without specifically knowing detailed information about them(column count, data types...). The user would input a connection string to his database, and all data from it would be copied to an internal DB.
I tried to achieve it by using SqlConnection and writing direct T-SQL queries and managed to write a script that creates empty tables in Internal database with correct columns:
string createDestinationTableQuery = "create table " + schemaName + ".[" + tableName + "](";
DataTable ColumnsDT = new DataTable();
string getTableColumnDataQuery = "SELECT * FROM "+originalDBName+".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'" + tableName +"'";
SqlCommand getTableColumnDataCommand = new SqlCommand(getTableColumnDataQuery, originalCon);
SqlDataAdapter TableDA = new SqlDataAdapter(getTableColumnDataCommand);
TableDA.Fill(ColumnsDT);
for (int x = 0; x < ColumnsDT.Rows.Count; x++)
{
createDestinationTableQuery += "[" + ColumnsDT.Rows[x].ItemArray[3].ToString() + "] " + "[" + ColumnsDT.Rows[x].ItemArray[7].ToString() + "], ";
}
createDestinationTableQuery = createDestinationTableQuery.Remove(createDestinationTableQuery.Length - 2);
createDestinationTableQuery += " )";
SqlCommand createDestinationTableCommand = new SqlCommand(createDestinationTableQuery, destinationCon);
createDestinationTableCommand.ExecuteNonQuery();
Console.WriteLine("Table " + schemaName + "." + tableName + " created succesfully!");
However, I am struggling with data insertion as the following code simply doesn't work:
DataTable dataTable = new DataTable();
string getTableDataquery = "select * from " + originalTableWithSchema;
SqlCommand getTableDataCommand = new SqlCommand(getTableDataquery, originalCon);
SqlDataAdapter da = new SqlDataAdapter(getTableDataCommand);
da.Fill(dataTable);
for (int x = 0; x < dataTable.Rows.Count; x++)
{
string insertQuery = "insert into " + schemaName + ".["+tableName+"](" ;
string values = "VALUES(";
for (int y = 0; y < dataTable.Columns.Count; y++)
{
insertQuery += dataTable.Columns[y].ColumnName + ", ";
values += dataTable.Rows[x].ItemArray[y].ToString() + ", ";
}
insertQuery = insertQuery.Remove(insertQuery.Length - 2);
insertQuery += " )";
values = values.Remove(values.Length - 2);
values += " )";
insertQuery += " " + values;
SqlCommand insertCommand = new SqlCommand(insertQuery, destinationCon);
insertCommand.ExecuteNonQuery();
}
da.Dispose();
How can I correctly Achieve this functionality? I was thinking of maybe scrapping all the code and using SMO instead?
If you are only looking to copy the data (because you have structure creation already working), then you could use DataTable to hold the data in a non-dbms specific structure, and a DataAdapter to generate the dbms specific insert statements. Here is an excerpt from code I wrote a while ago to copy data from Access to MySQL:
List<string> tableNames = new List<string>();
try
{
// Open connect to access db
sourceConn.Open();
// Build table names list from schema
foreach (DataRow row in sourceConn.GetSchema("Tables").Select("table_type = 'TABLE'"))
tableNames.Add(row["table_name"].ToString());
}
catch (Exception ex)
{
throw ex;
}
finally
{
if(sourceConn.State != ConnectionState.Closed)
sourceConn.Close();
}
foreach (string table in tableNames)
{
//Get all table data from Access
string query = string.Format("SELECT * FROM {0}", table);
DataTable accessTable = new DataTable(table);
try
{
sourceConn.Open();
System.Data.OleDb.OleDbCommand accessSqlCommand = new System.Data.OleDb.OleDbCommand(query, accessConn);
System.Data.OleDb.OleDbDataReader reader = (System.Data.OleDb.OleDbDataReader)accessSqlCommand.ExecuteReader();
// Load all table data into accessTable
accessTable.Load(reader);
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(sourceConn.State != ConnectionState.Closed)
sourceConn.Close();
}
// Import data into MySQL
accessTable.AcceptChanges();
// The table should be empty, so set everything as new rows (will be inserted)
foreach (DataRow row in accessTable.Rows)
row.SetAdded();
try
{
destConn.Open();
MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(query, mySqlConn);
MySql.Data.MySqlClient.MySqlCommandBuilder cb = new MySql.Data.MySqlClient.MySqlCommandBuilder(da);
da.InsertCommand = cb.GetInsertCommand();
// Update the destination table 128 rows at a time
da.UpdateBatchSize = 128;
// Perform inserts (and capture row counts for output)
int insertCount = da.Update(accessTable);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if(destConn.State != ConnectionState.Closed)
destConn.Close();
}
}
This could certainly be more efficient, but I wrote it for a quick conversion. Also, since this is copied and pasted you may need to tweak it. Hope it helps.
It might be worth thinking about using a linked server. Once a linked server is defined in the destination server, a table can be created and automatically filled with data using a SELECT…INTO statement.
Query executed in destination server database:
SELECT * INTO NewTableName FROM
SourceServername.SourceDatabasename.dbo.SourceTableName

C# SQL Insert with a very large number of parameters

I have had a problem for a few days and nothing online seems to do it.
I have an SQL table that has 150 columns. I am reading data from an ODBC connection and I want to insert that data into the SQL table. Basically duplicate the ODBC table as SQL.
My problem is that if I put everything in a string and insert it I face a hell of a time with escape characters and exceptions that I can't figure out.
Is there a way to parametrize my insert values that doesn't involve me naming each and every on of them separatly.
This is what I have right now. Also, if anyone knows an easier way to move an ODBC table to an SQL form please let me know
private void fillTable(string tableName)
{
String query = "SELECT * FROM " + tableName;
OdbcCommand command = new OdbcCommand(query, Program.myConnection);
OdbcDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
int columnCount = reader.FieldCount;
for (int i = 0; i < columnCount; i++)
{
SqlCommand sCommand = new SqlCommand("ALTER TABLE " + tableName + " ADD " + reader.GetName(i) + " varchar(MAX)", Program.myConnection2);
sCommand.ExecuteNonQuery();
}
string row="";
while (!reader.IsClosed)
{
try
{
row = "";
reader.Read();
for (int i = 0; i < columnCount; i++)
{
if (reader != null)
{
if (reader.GetString(i).Contains('\''))
{
Console.WriteLine("REPLACED QUOT");
String s = reader.GetString(i).Replace("\'", "A");
Console.WriteLine(s);
row += s;
}
else
{
row += "\'" + reader.GetString(i).Trim() + "\',";
}
// Console.WriteLine("FILLER: " + reader.GetString(i));
}
//Console.WriteLine(row);
}
//Console.WriteLine();
row = row.Substring(0, row.Length - 1);
SqlCommand insertCommand = new SqlCommand("INSERT INTO " + tableName + " VALUES(\'1\'," + row + ")", Program.myConnection2);
insertCommand.ExecuteNonQuery();
}
catch (SqlException exp)
{
Console.WriteLine(exp.StackTrace);
Console.WriteLine(row);
// this.Close();
}
}
Program.myConnection2.Close();
}
You can write a method that creats parameter names automatically, adds it to command, and returns the name so that you can use it in the query:
private int _paramCounter = 1;
private string CreateParameter(SqlCommand command, object value) {
string name = "#P" + _paramCounter.ToString();
_paramCounter++;
command.Parameters.AddWithValue(name, value);
return name;
}
Usage:
row += CreateParameter(insertCommand, reader.GetString(i).Trim()) + ",";
Note that you need to create the command object before you loop through the columns. Also, although not needed, you might want to reset the _paramCounter for each row, otherwise the parameter names get longer in the end.

Reading Text files and inserting the words present in those files into a table in sql server

I have a folder that contains 25000 text files and i would like to read these files and place the words into table.My text files are named in the following format 1.txt,2.txt,........and so on to 25000.txt. Each text file contains words in the following form.
sample contents of my file
apple
cat
rat
shoe
The words may be repeated in other textfiles too, i want a c# code that could read the text files identify the words that are repeated and also those that are not repeated and then insert them into database in Sqlserver in the following form.
keyword document name
cat 1.txt,2.txt,3.txt
rat 4.txt,1.txt
fish 5.txt
`
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace RAMESH
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
int i;
string sqlstmt,str;
SqlConnection con = new SqlConnection("data source=dell-pc\\sql1; initial catalog=db; user id=sa; password=a;");
SqlCommand cmd;
sqlstmt = "delete from Items";
cmd = new SqlCommand(sqlstmt, con);
con.Open();
cmd.ExecuteNonQuery();
for (i = 0; i < files.Length; i++)
{
StreamReader sr = new StreamReader(files[i]);
FileInfo f = new FileInfo(files[i]);
string fname;
fname = f.Name;
fname = fname.Substring(0, fname.LastIndexOf('.'));
//MessageBox.Show(fname);
while ((str = sr.ReadLine()) != null)
{
int nstr=1;
//int x,y;
//for (x = 0; x < str.Length; x++)
//{
// y = Convert.ToInt32(str.Substring(x,1));
// if ((y < 48 && y > 75) || (y < 65 && y > 97) || (y < 97 && y > 122)) ;
//}
sqlstmt = "insert into Items values('" + str + "','" + fname + "')";
cmd = new SqlCommand(sqlstmt, con);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
sqlstmt = "update Items set docname=docname + '," + fname + "' where itemname='" + str + "'";
cmd = new SqlCommand(sqlstmt, con);
cmd.ExecuteNonQuery();
}
}
sr.Close();
}
MessageBox.Show("keywords added successfully");
con.Close();
}
}
}
`
First and foremost I will add a Stored procedure to your database to isolate the logic for update or insert
CREATE PROCEDURE UpsertWords
#word nvarchar(MAX), #file nvarchar(256)
as
Declare #cnt integer
Select #cnt = Count(*) from Items where ItemName = #word
if #cnt = 0
INSERT INTO Items (#word, #file)
else
UPDATE Items SET docname = docname + ',' + #file where ItemName = #word
Now, we could simplify your code a lot
.....
// Build the command just one time, outside the loop,
// make it point to the stored procedure above
cmd = new SqlCommand("UpsertWords", con);
cmd.CommandType = CommandType.StoredProcedure;
// Create dummy parameters, the actual value is supplied inside the loop
cmd.Parameters.AddWithValue("#word", string.Empty);
cmd.Parameters.AddWithValue("#file", string.Empty);
// Now loop on every file
for (i = 0; i < files.Length; i++)
{
// Open and read all the lines in the current file
string[] lines = File.ReadAllLines(files[i]);
// Get only the filename part without the extension
string fname = Path.GetFileNameWithoutExtension(files[i])
// In case of just one line per file, this loop will execute just one time
// however we also could handle more than one line per file
foreach(string line in lines)
{
// Set the actual value of the parameters created outside the loop
cmd.Parameters["#word"] = line;
cmd.Parameters["#file"] = fname;
// Run the insert or update (the logic is inside the storedprocedure)
cmd.ExecuteNonQuery();
}
At this point is not clear if your line is composed of a single word or if you have more than one word separated by some character (tab, comma, semicolon). In that case you need to split the string and another loop.
However, I find your database schema wrong. It is better to add a new row for every word with the file in which it appears. In this way a simple query like
SELECT docname from Items where itemname = #word
will yeld all the files without any big performance problem and you have a more searchable database.
Or, if you need to count the occurence of a word
SELECT ItemName, COUNT(ItemName) as WordCount
FROM Items
GROUP BY ItemName
ORDER BY Count(ItemName) ASC
Try this approach:
First start with your Files, loop through and create a simple XML document.
var fname = "File12.txt";
var keywords = new List<string>(new[]{ "dog", "cat", "moose" });
var miXML = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("root"));
foreach (var el in keywords.Select(i => new XElement("item", new XAttribute("key", i))))
{
miXML.Root.Add(el);
}
using (var con = new SqlConnection("Server=localhost;Database=HT;Trusted_Connection=True;"))
{
con.Open();
using (var cmd = new SqlCommand("uspUpsert", con) {CommandType = CommandType.StoredProcedure})
{
cmd.Parameters.AddWithValue("#X", miXML.ToString());
cmd.Parameters.AddWithValue("#fileName", fname);
cmd.ExecuteNonQuery();
}
}
Then for your stored procedure you can call this Proc, which will convert that XML into a table, inserting the keywords and the file name into the database.
CREATE PROCEDURE uspUpsert
#X xml,
#Filename varchar(100)
AS
BEGIN
SET NOCOUNT ON;
WITH KV as (
select
x.v.value('#key', 'varchar(20)') as Keyword
,#FileName as FileName
FROM #x.nodes('/root/item') x(v)
)
insert into Items
select KV.keyWord, KV.FileName
from KV
left outer join Items I on I.Keyword=KV.Keyword and I.FileName=KV.FileName
where I.id is null
END
Since you likely don't want 'file1.txt file2.txt file3.txt' to find duplicates, you will use this query to find words in duplicate files:
select * from items where keyword='dog'
Alternatively, could now take a count and do all other aggregation on this table as well.

Insert DateTime into Access

The problem:
I'm trying to insert a date time into an access database using the Oledb interface in C#.
Hacking solution: Generate my on insert string without using command.Properties
I can insert text into the database with no problem, but when trying datetime, I end up with this error: System.Data.OleDb.OleDbException {"Data type mismatch in criteria expression."}
There are several posts similar to this but alas with no working solution.
Here is my code:
void TransferData()
{
string instCmd = Get_InsertCommand(0); // hard coded table 0 for testing
Fill_ProductTable_ToInsert();
con.Open();
// It would be nice not to have to separate the date indexes
int[] textIndex = { 0, 1, 2, 3, 4, 7 };
int[] dateIndex = { 5, 6 };
try
{
foreach (DataRow row in DataToStore.Tables[0].Rows)
{
OleDbCommand command = new OleDbCommand();
command.Connection = con;
command.CommandText = instCmd;
foreach(int j in textIndex)
command.Parameters.AddWithValue("#" + j, row[j]);
foreach (int j in dateIndex)
{
// TESTING CODE
///////////////////////////////////////////////////////////////////////////
string input = "#\'" +DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") +"\'#";
command.Parameters.AddWithValue("#" + j, input.ToString());
Program.WriteLine(input.ToString());
///////////////////////////////////////////////////////////////////////////
}
command.ExecuteNonQuery();
}
}
finally
{
con.Close();
}
}
string Get_InsertCommand(int i)
{
string sqlIns = "INSERT INTO " + DataToStore.Tables[0].TableName + " (";
string temp = "VALUES (";
for (int j = 0; j < expected_header[i].Length - 1; j++)
{
sqlIns += expected_header[i][j] + ", ";
temp += "#" + j + ", ";
}
int lastIndex = expected_header[i].Length -1;
sqlIns += expected_header[i][lastIndex] + ") ";
temp += "#" + lastIndex + ")";
sqlIns += temp;
return sqlIns;
}
Inside the area labeled testing code, I have tried every permutation of date time I could think of.
I tried every format with # and '
I tried these formats: yyyy-MM-dd, yyyyMMdd, yyyy\MM\dd, yyyy/MM/dd
I also tried ToOADate()
And ToString(), ToShortDateString()
I also tried setting the database to accept ANSI-92 Sql
I'm running out of ideas.
Note: This code is set up to deal with multiple tables from multiple databases, mind the loops...
Use parameters properly, and don't worry about the format of the datetime value that you concatenate in your query.
I don't understand why you want to convert the datetime value to a string value ?
DateTime theDate = new DateTime(2012,10,16);
var cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO sometable (column) VALUES (#p_bar)";
cmd.Parameters.Add ("#p_bar", OleDbType.DateTime).Value = theDate;
I was able to solve this issue by not using command properties. I generated my own sql input and set it to cmd.commandText. The text input for datetime to a data base is #yyyy-MM-dd#

Categories

Resources