D
D
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.Odbc;
using System.Collections;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
OdbcConnection dbconn = new
OdbcConnection("Provider=MSDASQL.1;DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=c:\\hedgefunds\\db1.mdb;DATABASE=db1;UID=root;PWD=;
OPTION=1;");
dbconn.Open();
OdbcDataAdapter dbrs = new OdbcDataAdapter("Select * From
MyTable;", dbconn);
OdbcCommandBuilder builder = new OdbcCommandBuilder(dbrs);
DataSet dbADOrs = new DataSet();
InitializeComponent();
dbrs.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dbrs.Fill(dbADOrs);
OpenReturnFile(dbrs, dbADOrs);
}
public void OpenReturnFile(DbDataAdapter dbrs, DataSet dbADOrs)
{
StreamReader sfp;
sfp = File.OpenText("c:\\temp\\updatenewfile.txt");
string buffer;
string[] fields;
char[] delims={'\t'};
while (sfp.EndOfStream == false)
{
buffer = sfp.ReadLine();
fields=buffer.Split(delims,
StringSplitOptions.RemoveEmptyEntries);
UpdateinDB(dbrs, dbADOrs, fields);
}
}
public void UpdateinDB(DbDataAdapter 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(dbADOrs);//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.InnerException.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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.Odbc;
using System.Collections;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
OdbcConnection dbconn = new
OdbcConnection("Provider=MSDASQL.1;DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=c:\\hedgefunds\\db1.mdb;DATABASE=db1;UID=root;PWD=;
OPTION=1;");
dbconn.Open();
OdbcDataAdapter dbrs = new OdbcDataAdapter("Select * From
MyTable;", dbconn);
OdbcCommandBuilder builder = new OdbcCommandBuilder(dbrs);
DataSet dbADOrs = new DataSet();
InitializeComponent();
dbrs.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dbrs.Fill(dbADOrs);
OpenReturnFile(dbrs, dbADOrs);
}
public void OpenReturnFile(DbDataAdapter dbrs, DataSet dbADOrs)
{
StreamReader sfp;
sfp = File.OpenText("c:\\temp\\updatenewfile.txt");
string buffer;
string[] fields;
char[] delims={'\t'};
while (sfp.EndOfStream == false)
{
buffer = sfp.ReadLine();
fields=buffer.Split(delims,
StringSplitOptions.RemoveEmptyEntries);
UpdateinDB(dbrs, dbADOrs, fields);
}
}
public void UpdateinDB(DbDataAdapter 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(dbADOrs);//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.InnerException.ToString());
fout.Close();
}
}
}
}
}