2 q413107364 Q413107364 于 2015.06.15 12:06 提问

Xamarin Studio 访问数据库失败,错误代码已知道,但不知怎么修改!

public string getWapCurrVersion111()
{
SQLiteDatabase dtBack;
string location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string dataBasePath = Path.Combine(location, "T_PUB_VERSION");
if (File.Exists(dataBasePath))
{
dtBack = SQLiteDatabase.OpenOrCreateDatabase(dataBasePath, null);
dtBack.ExecSQL("CREATE TABLE IF NOT EXISTS T_PUB_VERSION (DataVersion REAL)");
dtBack.ExecSQL("INSERT INTO T_PUB_VERSION (DataVersion) VALUES (0.1);");
}
else { dtBack = SQLiteDatabase.OpenDatabase(dataBasePath, null, DatabaseOpenFlags.OpenReadwrite); }
Android.Database.ICursor MyCur = dtBack.RawQuery("SELECT DataVersion FROM T_PUB_VERSION", null);
dtBack.Close();
return ((MyCur == null || MyCur.MoveToFirst() == false) ? "" : MyCur.GetString(0));

        return (dataBasePath + (File.Exists(dataBasePath) ? "HAVETT" : "NOTT"));
    }

出错代码:
dtBack = SQLiteDatabase.OpenOrCreateDatabase(dataBasePath, null);
出错后直接就退出了,用TRY EXCEPT也没用

附 换用MONO提供的包,还是不能访问:
public string getWapCurrVersion()
{
try
{
string DatabaseName = "PUB.db3";
string documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string db = Path.Combine(documents, DatabaseName);
if (!File.Exists(db))
{
#region 创建库
SqliteConnection.CreateFile(db);
var connCreate = new SqliteConnection("Data Source=" + db);
var commands = new[] { //"DROP TABLE IF EXISTS TWITTERDATA",
"CREATE TABLE IF NOT EXISTS T_PUB_VERSION (DataVersion VARCHAR(8) )",
"INSERT INTO T_PUB_VERSION (DataVersion) VALUES ('0.1');" };

                foreach (var cmd in commands)
                {
                    var sqlitecmd = connCreate.CreateCommand();
                    sqlitecmd.CommandText = cmd;
                    sqlitecmd.CommandType = CommandType.Text;
                    connCreate.Open();
                    sqlitecmd.ExecuteNonQuery();
                    connCreate.Close();
                }
                connCreate.Dispose();
                #endregion
            }
            var conn = new SqliteConnection("Data Source=" + db);
            SqliteCommand sqlc = new SqliteCommand();
            sqlc.Connection = conn;
            conn.Open();
            sqlc.CommandText = "SELECT DataVersion FROM T_PUB_VERSION ";
            sqlc.CommandType = CommandType.Text;
            SqliteDataReader sdr = sqlc.ExecuteReader();
            string strBack = "NNN";
            if (sdr.Read())
            {
                strBack = sdr["DataVersion"].ToString();
            }
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            conn.Dispose();

            return strBack;
        }
        catch (Exception Ex)
        {
            return Ex.ToString();
        }

    }
Csdn user default icon
上传中...
上传图片
插入图片