简体   繁体   中英

local database won't update and doesn't show errors

Hey i'm new to this and from what i managed to pick up this should be working but it doesn't update my local database.

I have a TelemarketingDatabaseDataSet that was auto generated when i created my local database, which then i dragged the table onto the dataset and i guess they're linked.

Now i have this code :

SqlCeConnection connection = new SqlCeConnection();
connection.ConnectionString = TelemarketingTracker.Properties.Settings.Default.TelemarketingDatabaseConnectionString;
TelemarketingDatabaseDataSet ds = new TelemarketingDatabaseDataSet();
// DataTable tbl = new DataTable();

SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from Calls", connection);
//adapter.InsertCommand = new SqlCeCommand("InsertQuery", connection);
adapter.Fill(ds,"Calls");
DataTable tbl = ds.Tables["Calls"];
//tbl.Columns.Add("caller");
//tbl.Columns.Add("called");
//tbl.Columns.Add("duration");
//tbl.Columns.Add("time");
var row = tbl.NewRow();

row[1] = Convert.ToString(caller);
row[2] = Convert.ToString(called);
row[3] = Convert.ToString(duration);
row[4] = Convert.ToDateTime(time);
tbl.Rows.Add(row);
adapter.Update(ds, "Calls");
connection.Close();
MessageBox.Show("Database should be updated!");

And please, i'm not intrested in using an SqlCommand as i prefer using DataSet.

Could the problem be related to datatypes of my table? it doesn't show errors to suggest that but i guess this could be the problem. my Table consists of :

ID - int,key caller - varchar called - varchar duration - varchar time - datetime

EDIT:

Now if i uncomment the insertQuery row i get an unhandled error occured in Syste.Data dll.

Now even if i try to use a regular insert command i get no errors but the database won't update. if this makes any diffrence after i close the debugging window i see an X next to the local database but it doesn't show any errors.

This is the command i've tried :

    using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Calls (caller, called, duration, time) Values(@Caller,@Called,@Duration,@Time)", connection))
{
    com.Parameters.AddWithValue("@Caller", row[1]);
    com.Parameters.AddWithValue("@Called", row[2]);
    com.Parameters.AddWithValue("@Duration", row[3]);
    com.Parameters.AddWithValue("@Time", row[4]);


    com.ExecuteNonQuery();
}

The Fill() method "Adds or refreshes rows in the DataSet to match those in the data source." The key part of this sentence being "to match those in the data source". The row you're adding gets wiped out when you call Fill() because it's not already in the source.

I'm not positive, but I don't think that you need to even call Fill() if you're only adding new records and not worried about modifying/removing existing ones. If you do need to call it though, it would obviously need to be moved before any new record insertions you make.

Try something similar to this..

string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\TelemarketingDatabase.sdf";
SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);

TelemarketingDatabaseDataSet ds = new TelemarketingDatabaseDataSet();   
SqlCeDataAdapter adapter = new SqlCeDataAdapter();
string qry = @"select * from Calls";
da.SelectCommand = new SqlCommand(qry, connection);
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
adapter.Fill(ds,"Calls");


DataTable tbl = ds.Tables["Calls"];
var row = tbl.NewRow();
row[0] = caller;
row[1] = called;
row[2] = duration;
row[3] = Convert.ToDateTime(time);
tbl.Rows.Add(row);
adapter.Update(ds,"Calls");

See the example here http://www.java2s.com/Code/CSharp/Database-ADO.net/UseDataTabletoupdatetableinDatabase.htm

Well in the end i didn't manage to solve this, instead i used a remote database and regular sql commands.

Thanks for those who helped!

just want to share this even the if the question is old

using System;
using System.IO; //needed for path.getdirectoryname() and directory.getcurrentdirectory()

string path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));

AppDomain.CurrentDomain.SetData("DataDirectory", path);

Directory.GetcurrentDirectory() will output "C:/..projectname/bin/debug" which is where the temporary database.mdf is located

by using Path.GetDirectoryName(Directory.GetcurrentDirectory()) it will give the directory of the current directory thus moving one location back "C:/..projectname/bin"

then use it again

Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) will give you the location of the root database in your project folder "C:/..projectname"

then just use AppDomain.CurrentDomain.SetData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM