![](/img/trans.png)
[英]How can I make an instance of a class created in one method available to another peer method?
[英]How can I make a SQLiteConnection object available to another method?
我试图在SQLite连接方法中使用SQLiteConnection对象,并将其提供给稍后在我的代码中使用的方法。
private void SelectDbRecords()
{
SQLiteConnection con;
SQLiteDataAdapter da;
SQLiteCommand cmd;
DataSet ds;
if (File.Exists("data.db"))
{
con = new SQLiteConnection("Data Source=data.db;Version=3;");
SetConnection();
con.Open();
string sql = "SELECT * FROM Sites";
cmd = con.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
...
如何使此con对象持久存在,以便以后可以访问它? 如果这是一个愚蠢的问题,我深表歉意。 我正在学习C#,似乎无法找到有关如何实现此目标的明确答案。 我尝试使用Friend,但似乎无法正常工作。
您可以在类内部全局声明con对象,以便以后可以使用该类的任何方法对其进行访问。
Public class classname
{
SQLiteConnection con = new SQLiteConnection("Data Source=data.db;Version=3;");
--Methods
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.