C# c数据库连接类中的ObjectDisposedException
我是C语言的新手,我正在用C语言编写一个需要连接sqlite数据库的应用程序。我认为创建自己的DBConnection类对我来说是最简单的,我在下面给出了这个类。目前我遇到的主要问题是析构函数。有时,当我实例化这个类时,它超出了范围,我会得到一个 System.ObjectDisposedException;'无法访问已释放的对象。 对象名称:“SQLiteConnection.” 现在我已经找到了这意味着什么的定义,但我真的不明白 该对象被实例化为wpf应用程序窗口的一部分,该异常在窗口被x'd移出后以及数据库操作完成后抛出C# c数据库连接类中的ObjectDisposedException,c#,wpf,exception,objectdisposedexception,C#,Wpf,Exception,Objectdisposedexception,我是C语言的新手,我正在用C语言编写一个需要连接sqlite数据库的应用程序。我认为创建自己的DBConnection类对我来说是最简单的,我在下面给出了这个类。目前我遇到的主要问题是析构函数。有时,当我实例化这个类时,它超出了范围,我会得到一个 System.ObjectDisposedException;'无法访问已释放的对象。 对象名称:“SQLiteConnection.” 现在我已经找到了这意味着什么的定义,但我真的不明白 该对象被实例化为wpf应用程序窗口的一部分,该异常在窗口被x'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Data.SQLite;
using System.IO;
namespace PhotoSuppliesInc
{
//this class will be used for connecting to the database in the various forms
public class DBConnector
{
//----------------------------------PRIVATES------------------------------//
//this is the connection to the db
private SQLiteConnection dbConnection;
//-------------------CONSTRUCTOR(S) and DESCTRUCTOR----------------//
public DBConnector()
{
string connectionString = "Data Source=C:\\Users\\dmand\\source\\repos\\PhotoSuppliesInc\\PhotoSuppliesInc\\database\\riverfrontphoto.db;" +
"Version = 3; FailIfMissing=True; Foreign Keys=True;";
try
{
dbConnection = new SQLiteConnection(connectionString);
dbConnection.Open();
MessageBox.Show("Database connected successfully");
}//end try
catch (SQLiteException e) { MessageBox.Show("error opening database"); }
}//end constructor
//destructor removes connection to database
~DBConnector()
{
dbConnection.Close();
}
//--------------------------------------------------GETTER(S)------------------------//
//public string Get_Filepath() { return filepath; }
//--------------------------------------------------UTILITY FUNCTIONS----------------//
public List<string> Select_Query(string query_string, string columns)
{
//the names of the columns in the select statement
string[] selection_columns = columns.Split();
//each member of the array is the tuple, the data is separated by spaces
List<string> result = new List<string>();
//this string constructs the result line to be inserted into the result list
string tempstring;
SQLiteCommand command = new SQLiteCommand(query_string, dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
tempstring = "";//reset the temp string for each tuple
int i = 0;
foreach(string s in selection_columns)
{
tempstring += reader[s] + " ";
i++;
}
//I'm not sure why result.add must go here, but it does
result.Add(tempstring);
}
//the result is a list who's contents are strings that represent each tuple
return result;
}
}//end class
}//end namespace
///////in LoginWindow.xaml.cs////
正如我所说的,我不知道这个错误是什么意思,或者我是否正确地使用了这种语言中的析构函数。在我看来,这里应该没有问题。有人能帮我弄清楚这个错误在我的课堂上意味着什么吗?
谢谢大家抽出时间。您没有正确使用析构函数,因为C没有析构函数,它有终结器 正确的方法是在using块中实现和访问类的实例 ~syntax表示的终结器是非确定性的-您无法控制垃圾收集器何时运行终结器,或者它是否运行终结器。在using块中实现IDisposable和访问实例可以实现资源的确定性清理
这就是得到ObjectDisposedException的原因:在释放SQLiteConnection实例之后,类的终结器正在运行 我忘了提到visual studio说异常发生在我编写的析构函数中。您没有正确使用析构函数,因为C没有析构函数,它有终结器。正确的方法是实现IDisposable模式,并使用块访问类的实例。~syntax表示的终结器是非确定性的-您无法控制垃圾收集器何时运行终结器,或者它是否运行终结器。在using块中实现IDisposable和访问实例可以实现资源的确定性清理。谢谢@DanielMann!我读了很多关于终结器和IDispose的书。我相信我已经解决了这个问题。再次感谢您抽出时间
public LoginWindow()
{
itializeComponent();
//testing the DBConnector class:
DBConnector riverfront = new DBConnector();
string output_string = "";
foreach (string s in riverfront.Select_Query(@"select distinct fname || ' ' || lname as 'Full_Name', country from employee, employee_account limit 5", "Full_Name country"))
output_string += s + "\n";
MessageBox.Show(output_string);
}
...