我正在尝试返回一个阅读器,以便我可以在另一堂课中阅读它。当我调用它时: return cmd.ExecuteReader(); 我得到一个 Mono.Data.Sqlite.SqliteException: 当我尝试调用插入查询并调用 cmd.ExecuteNonQuery(); 时也会发生同样的事情。我有以下课程:
数据库.cs:
public class Database
{
private SqliteConnection connection;
public string connectiondata;
private static string _LocalDatabasePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "LocalData.db3");
public Database(string ConnectionString)
{
this.connection = new SqliteConnection(ConnectionString);
}
public Database(SqliteConnection Connection)
{
this.connection = Connection;
}
public static UsersDatabase User { get { return new UsersDatabase("Data Source=" + _LocalDatabasePath); } }
#region Queries
protected SqliteDataReader SelectQuery(string columns, string table, string condition)
{
if (this.Connected)
{
SqliteCommand cmd = this.connection.CreateCommand();
cmd.CommandText = "SELECT " + columns + " FROM " + table + (string.IsNullOrEmpty(condition) ? "" : " WHERE " + condition);
cmd.CommandType = CommandType.Text;
return cmd.ExecuteReader();
}
else
{
throw new Exception("Inte ansluten till databasen");
}
}
protected SqliteDataReader SelectQuery(string columns, string table)
{
return this.SelectQuery(columns, table, "");
}
protected void InsertQuery(string table, string columns, string values)
{
if (this.Connected)
{
SqliteCommand cmd = this.connection.CreateCommand();
cmd.CommandText = "INSERT INTO " + table + "(" + columns + ") VALUES(" + values + ")";
cmd.ExecuteNonQuery();
}
else
{
throw new Exception("Inte ansluten till databasen");
}
}
#endregion
#region Connection
public bool Connected
{
get
{
if (this.connection == null)
{
return false;
}
if (this.connection.State == ConnectionState.Open)
{
return true;
}
return false;
}
}
public void Connect()
{
if (!this.Connected)
{
string db = _LocalDatabasePath;
bool exists = File.Exists(db);
if (!exists)
{
SqliteConnection.CreateFile(db);
}
this.connection = new SqliteConnection("Data Source=" + db);
this.connection.Open();
}
}
public void Close()
{
if (this.Connected)
{
this.connection.Close();
}
}
#endregion
}
}
用户数据库.cs:
public class UsersDatabase : Database
{
public UsersDatabase(string connectionstring)
: base(connectionstring)
{
}
public UsersDatabase(SqliteConnection connection)
: base(connection)
{
}
public void CreateUser()
{
this.Connect();
this.InsertQuery("People", "PersonID, FirstName, LastName", "'" + "namn11" + "', '" + "enamn11" + "'");
this.Close();
}
public UserStruct[] GetAllUsers()
{
List<UserStruct> templist = new List<UserStruct>();
this.Connect();
SqliteDataReader reader = this.SelectQuery("*", "People", "");
while (reader.Read())
{
UserStruct tmpuser = new UserStruct();
tmpuser.ID = reader["PersonID"].ToString();
tmpuser.Name = reader["FirstName"].ToString();
tmpuser.LastName = reader["LastName"].ToString();
templist.Add(tmpuser);
}
this.Close();
return templist.ToArray();
}
}
活动1.cs:
[Activity(Label = "example_proj", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button btn2 = FindViewById<Button>(Resource.Id.btn2);
btn2.Click += new EventHandler(btn2_Click);
}
void btn2_Click(object sender, EventArgs e)
{
UserStruct[] tmpusr = UsersDatabase.User.GetAllUsers();//get usrs
}
}
所以...从活动中,我调用 UsersDatabase GetAllUsers() 或 CreateUser()。UsersDatabase 使用 Database 类来帮助查询和连接。因此,从 UsersDatabase 我首先调用 Connect() 方法来建立与 DB 的连接,然后我调用例如 SelectQuery() 来返回一个读取器,我稍后将在 UsersDatabase 类中读取该读取器并用正在执行的信息填充一个结构要返回到活动1。
正如我所说,当我遇到异常时,我会尽可能地返回读者。连接打开并且数据库存在。