I am trying to add records to a database and getting no errors but the
databse is not updated.
Not sure what is wrong maybe in my function I'm losing some thing?
What do you think? My code is below.
Thanks
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows. Forms;
using System.Data.Com mon;
using System.Data.Odb c;
using System.Collecti ons;
using System.IO;
namespace WindowsFormsApp lication1
{
public partial class Form1 : Form
{
public Form1()
{
OdbcConnection dbconn = new
OdbcConnection( "Provider=MSDAS QL.1;DRIVER={Mi crosoft Access Driver
(*.mdb)};DBQ=c: \\hedgefunds\\d b1.mdb;DATABASE =db1;UID=root;P WD=;
OPTION=1;");
dbconn.Open();
OdbcDataAdapter dbrs = new OdbcDataAdapter ("Select * From
MyTable;", dbconn);
OdbcCommandBuil der builder = new OdbcCommandBuil der(dbrs);
DataSet dbADOrs = new DataSet();
InitializeCompo nent();
dbrs.MissingSch emaAction = MissingSchemaAc tion.AddWithKey ;
dbrs.Fill(dbADO rs);
OpenReturnFile( dbrs, dbADOrs);
}
public void OpenReturnFile( DbDataAdapter dbrs, DataSet dbADOrs)
{
StreamReader sfp;
sfp = File.OpenText(" c:\\temp\\updat enewfile.txt");
string buffer;
string[] fields;
char[] delims={'\t'};
while (sfp.EndOfStrea m == false)
{
buffer = sfp.ReadLine();
fields=buffer.S plit(delims,
StringSplitOpti ons.RemoveEmpty Entries);
UpdateinDB(dbrs , dbADOrs, fields);
}
}
public void UpdateinDB(DbDa taAdapter dbrs, DataSet dbADOrs,
String[] fields)
{
DataTable dt = dbADOrs.Tables[0];
DataRow rec = dt.NewRow();
MessageBox.Show (dt.Columns[0].ToString());
if (fields[0] != "fundname")//file has header so ignore it.
{
rec["name"] = fields[0];
rec["date"] = fields[1];
rec["corr"] = fields[2];
try
{
dbrs.Update(dbA DOrs);//no update to the database occurs
here no exceptions triggered
}
catch (Exception e)
{
StreamWriter fout = new
StreamWriter("c :\\temp\\status .txt", true);
fout.WriteLine( "Error at " + fields[0].ToString()+ "
" + e.InnerExceptio n.ToString());
fout.Close();
}
}
}
}
}
databse is not updated.
Not sure what is wrong maybe in my function I'm losing some thing?
What do you think? My code is below.
Thanks
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows. Forms;
using System.Data.Com mon;
using System.Data.Odb c;
using System.Collecti ons;
using System.IO;
namespace WindowsFormsApp lication1
{
public partial class Form1 : Form
{
public Form1()
{
OdbcConnection dbconn = new
OdbcConnection( "Provider=MSDAS QL.1;DRIVER={Mi crosoft Access Driver
(*.mdb)};DBQ=c: \\hedgefunds\\d b1.mdb;DATABASE =db1;UID=root;P WD=;
OPTION=1;");
dbconn.Open();
OdbcDataAdapter dbrs = new OdbcDataAdapter ("Select * From
MyTable;", dbconn);
OdbcCommandBuil der builder = new OdbcCommandBuil der(dbrs);
DataSet dbADOrs = new DataSet();
InitializeCompo nent();
dbrs.MissingSch emaAction = MissingSchemaAc tion.AddWithKey ;
dbrs.Fill(dbADO rs);
OpenReturnFile( dbrs, dbADOrs);
}
public void OpenReturnFile( DbDataAdapter dbrs, DataSet dbADOrs)
{
StreamReader sfp;
sfp = File.OpenText(" c:\\temp\\updat enewfile.txt");
string buffer;
string[] fields;
char[] delims={'\t'};
while (sfp.EndOfStrea m == false)
{
buffer = sfp.ReadLine();
fields=buffer.S plit(delims,
StringSplitOpti ons.RemoveEmpty Entries);
UpdateinDB(dbrs , dbADOrs, fields);
}
}
public void UpdateinDB(DbDa taAdapter dbrs, DataSet dbADOrs,
String[] fields)
{
DataTable dt = dbADOrs.Tables[0];
DataRow rec = dt.NewRow();
MessageBox.Show (dt.Columns[0].ToString());
if (fields[0] != "fundname")//file has header so ignore it.
{
rec["name"] = fields[0];
rec["date"] = fields[1];
rec["corr"] = fields[2];
try
{
dbrs.Update(dbA DOrs);//no update to the database occurs
here no exceptions triggered
}
catch (Exception e)
{
StreamWriter fout = new
StreamWriter("c :\\temp\\status .txt", true);
fout.WriteLine( "Error at " + fields[0].ToString()+ "
" + e.InnerExceptio n.ToString());
fout.Close();
}
}
}
}
}
Comment