繁体   English   中英

本地数据库不会更新,也不会显示错误

[英]local database won't update and doesn't show errors

嘿,我对此并不陌生,从我设法了解的内容来看,它应该可以正常工作,但它不会更新我的本地数据库。

我有一个TelemarketingDatabaseDataSet ,它是在创建本地数据库时自动生成的,然后我将该表拖到数据集上,并猜测它们已链接。

现在我有此代码:

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!");

而且,我不喜欢使用SqlCommand,因为我更喜欢使用DataSet。

问题可能与表的数据类型有关吗? 它没有显示错误提示,但是我想这可能是问题所在。 我的表包括:

ID - INT,关键主叫 - VARCHAR 称为 - VARCHAR 时间 - VARCHAR 时间 -日期时间

编辑:

现在,如果我取消注释insertQuery行,则会在Syste.Data dll中发生未处理的错误。

现在,即使我尝试使用常规插入命令,也不会出现任何错误,但数据库不会更新。 如果在关闭调试窗口后出现任何差异,我会在本地数据库旁边看到一个X,但它没有显示任何错误。

这是我尝试过的命令:

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

Fill()方法“添加或刷新DataSet中的行以匹配数据源中的行。” 这句话的关键部分是“匹配数据源中的那些”。 调用Fill()时,要添加的行会被擦除,因为该行尚未在源代码中。

我还不能肯定,但我不认为你需要甚至称之为填写()如果你只添加新记录,而不是担心修改/删除现有的。 如果确实需要调用它,则显然需要在插入任何新记录之前将其移动。

尝试类似的事情。

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");

在此处查看示例http://www.java2s.com/Code/CSharp/Database-ADO.net/UseDataTabletoupdatetableinDatabase.htm

最后,我没有设法解决这个问题,而是使用了远程数据库和常规的sql命令。

感谢您的帮助!

即使问题很旧也只想分享

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()将输出“ C:/ .. projectname / bin / debug”,这是临时database.mdf所在的位置

通过使用Path.GetDirectoryName(Directory.GetcurrentDirectory()),它将给出当前目录的目录,从而将一个位置移回“ C:/ .. projectname / bin”

然后再次使用

Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))将为您提供根数据库在项目文件夹“ C:/ .. projectname”中的位置

然后只需使用AppDomain.CurrentDomain.SetData()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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