471,120 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 software developers and data experts.

database wont update

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={Microso ft 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();
}
}
}
}

}

Jun 27 '08 #1
1 1105
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=MSDASQL.1;DRIVER={Microso ft 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.

--
J.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Mark Harrison | last post: by
1 post views Thread by =?Utf-8?B?UmljaA==?= | last post: by
30 posts views Thread by fniles | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.