So I have been using oledb to create an Access table and insert data into it but I recently hit a brick wall when the data I was trying to insert became longer than 255 characters in some fields. This is apparently not possible to do.
I ran into this problem in a past program when doing the same type of thing but with Excel. The solution I found was to use Microsoft.Office.Interop.Excel. This actually made my program a lot shorter and simplified, so I figured I could do it with an Access database as well.
Only problem is, there is almost no documentation or example code for this reference so I have no idea how to complete my task. Can someone provide me with some example code on how to even just generally use Microsoft.Office.Interop.Access, or even more helpful would be some example code on how to create a table in an existing database, and fill that table with data?
UPDATE: Here's my oledb code that is not working:
var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Db);
var cnn = new OleDbConnection(cnnStr);
cnn.Open();
//make new access table
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = cnn;
command.CommandText = String.Format("CREATE TABLE [{0}] ([Tag] string, [Text] string)", oldTable + "_combined");
try
{
command.ExecuteNonQuery();
}
catch
{
//table already exists, so delete it and remake it
command.CommandText = String.Format("DROP TABLE [{0}]", newTable + "_combined");
command.ExecuteNonQuery();
command.CommandText = String.Format("CREATE TABLE [{0}] ([Tag] string, [Text] string)", oldTable + "_combined");
command.ExecuteNonQuery();
}
}
//fill access table
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = cnn;
command.CommandText = String.Format("INSERT INTO [{0}] (Tag, [Text]) VALUES (?, ?)", oldTable + "_combined");
command.Parameters.Add(new OleDbParameter("Tag", ""));
command.Parameters.Add(new OleDbParameter("Text", ""));
for (int i = 0; i < (comboText.Length - 1); i++)
{
command.Parameters["Tag"].Value = comboTag[i].Replace("\"", "");
comboText[i] = comboText[i].Replace("\r\n", "");
command.Parameters["Text"].Value = comboText[i].Replace("\"", "");
command.ExecuteNonQuery();
}
}
cnn.Close();
When my code reaches the 19th element in the comboText array (this element has 273 characters), the code errors and stops on command.ExecuteNonQuery() with the following error message: "OleDbException was unhandled: The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data."