database wont update

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();
}
}


}
}

}
 
J

Jeroen Mostert

D said:
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.
OdbcConnection dbconn = new
OdbcConnection("Provider=MSDASQL.1;DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=c:\\hedgefunds\\db1.mdb;DATABASE=db1;UID=root;PWD=;
OPTION=1;");

Your problem's right there: you're using Access. Use a real database
instead. SQL Server Express is free. So's SQL Server Compact Edition if you
need the ability to exchange databases as files. Snarking aside, though...
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

You forgot to call .AddRow(). This always trips up newcomers (I speak from
experience), but .NewRow() doesn't actually add a new row to the table, it
just creates one based on the table's scheme.

See http://msdn.microsoft.com/library/z16c79x4 for a sample.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top