簡體   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