database wont update

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D

    database wont update

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


    }
    }

    }



  • Jeroen Mostert

    #2
    Re: database wont update

    D wrote:
    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.
    >
    <snip>
    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;");
    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(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
    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.

    --
    J.

    Comment

    Working...