PC Review


Reply
Thread Tools Rate Thread

Adding data columns to a disconnected database

 
 
canoewhiteh2o
Guest
Posts: n/a
 
      2nd Jan 2008
I am having trouble adding data columns to a disconnected database. I
first load a datatable using:
private DataTable dtMacros = new DataTable();
private ArrayList macro = new ArrayList();
private SQLiteConnection cn;
private SQLiteDataAdapter adapterMacros;
cn = new SQLiteConnection("Data Source = irDatabase.db3");
SQLiteCommand commandMacros = new SQLiteCommand();
commandMacros.CommandText = "SELECT * FROM irMacros";
adapterMacros = new SQLiteDataAdapter(commandMacros.CommandText,
cn);
SQLiteCommandBuilder builderMacros = new
SQLiteCommandBuilder(adapterMacros);
adapterMacros.Fill(dtMacros);
dtMacros.PrimaryKey = new DataColumn[]
{ dtMacros.Columns["MacroName"] };

I am using an arraylist (macro) to store values for a new row - all
values are strings.

Later I update the data with the following code:
private void UpdateMacroDatabase(ArrayList macro, string macroName)
{
//Add datatable columns if needed for the datatable in
memory
if (dtMacros.Columns.Count <= macro.Count)
{
int i = dtMacros.Columns.Count;
while (i <= macro.Count)
{
dtMacros.Columns.Add("Command" + (i-1).ToString(),
typeof(String));
i++;
}
}
//update the datatable in memory
DataRow row = dtMacros.Rows.Find(macroName);
if (row == null)
{
row = dtMacros.NewRow();
row[0] = macroName;
for (int i = 0; i < macro.Count; i++)
{
row[i+1] = macro[i];
}
dtMacros.Rows.Add(row);
}
else
{
row["MacroName"] = macroName;
for (int i = 1; i <= macro.Count; i++)
{
row[i] = macro[i - 1];
}
}
adapterMacros.Update(dtMacros);
dtMacros.AcceptChanges();
macro.Clear();
}

The datatable in memory adds the new columns and rows as it should.
The database on disk will add the new rows and will update the
columns, but will not add the new columns.

Thanks for any help.

Regards,
Gary B


 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      2nd Jan 2008
Hi,


You need to add the columns in the "real" DB, which makes me wonder, why
they are not there in the first place?

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"canoewhiteh2o" <(E-Mail Removed)> wrote in message
news:4e39f7c4-81bf-47f8-b2aa-(E-Mail Removed)...
>I am having trouble adding data columns to a disconnected database. I
> first load a datatable using:
> private DataTable dtMacros = new DataTable();
> private ArrayList macro = new ArrayList();
> private SQLiteConnection cn;
> private SQLiteDataAdapter adapterMacros;
> cn = new SQLiteConnection("Data Source = irDatabase.db3");
> SQLiteCommand commandMacros = new SQLiteCommand();
> commandMacros.CommandText = "SELECT * FROM irMacros";
> adapterMacros = new SQLiteDataAdapter(commandMacros.CommandText,
> cn);
> SQLiteCommandBuilder builderMacros = new
> SQLiteCommandBuilder(adapterMacros);
> adapterMacros.Fill(dtMacros);
> dtMacros.PrimaryKey = new DataColumn[]
> { dtMacros.Columns["MacroName"] };
>
> I am using an arraylist (macro) to store values for a new row - all
> values are strings.
>
> Later I update the data with the following code:
> private void UpdateMacroDatabase(ArrayList macro, string macroName)
> {
> //Add datatable columns if needed for the datatable in
> memory
> if (dtMacros.Columns.Count <= macro.Count)
> {
> int i = dtMacros.Columns.Count;
> while (i <= macro.Count)
> {
> dtMacros.Columns.Add("Command" + (i-1).ToString(),
> typeof(String));
> i++;
> }
> }
> //update the datatable in memory
> DataRow row = dtMacros.Rows.Find(macroName);
> if (row == null)
> {
> row = dtMacros.NewRow();
> row[0] = macroName;
> for (int i = 0; i < macro.Count; i++)
> {
> row[i+1] = macro[i];
> }
> dtMacros.Rows.Add(row);
> }
> else
> {
> row["MacroName"] = macroName;
> for (int i = 1; i <= macro.Count; i++)
> {
> row[i] = macro[i - 1];
> }
> }
> adapterMacros.Update(dtMacros);
> dtMacros.AcceptChanges();
> macro.Clear();
> }
>
> The datatable in memory adds the new columns and rows as it should.
> The database on disk will add the new rows and will update the
> columns, but will not add the new columns.
>
> Thanks for any help.
>
> Regards,
> Gary B
>
>



 
Reply With Quote
 
canoewhiteh2o
Guest
Posts: n/a
 
      6th Jan 2008
Well they are not there in the first place because I am allowing the
user to increase the number of columns based on user need. I am using
the db to store mouse clicks within a macro.

Anyway, I did figured it out. I threw away the CommandBuilder and
constructed my on sql strings. Probably should have taken that
approach in the first place.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving Data to a Disconnected (Temporarily Unreachable) Database Charles Microsoft Dot NET 35 5th Jun 2009 04:33 PM
Saving Data to a Disconnected (Temporarily Unreachable) Database Charles Microsoft Dot NET 0 23rd May 2009 06:48 PM
Adding Columns in Access Database =?Utf-8?B?S2FybA==?= Microsoft Access VBA Modules 1 1st Jun 2007 03:25 AM
Adding Columns to Jet/Access database johnb41 Microsoft VB .NET 11 5th Jul 2005 08:40 PM
Adding columns to database table Oli Microsoft ADO .NET 3 30th Jan 2004 02:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:50 AM.