如何在C#中将数据从Sqlite设置为TextView?

时间:2015-05-27 07:14:08

标签: c# android sqlite xamarin.android

我想将数据库中已经给出的值分配给我创建的联系表单。在开始时我使用注册表格将数据存储在数据库中,我现在需要从该表中获取两个值名称和电子邮件,并在联系表单的Texview中给出它是使用的代码。

    public void AddRecord(string sName, string sEmail, int iAge, string sCity)
    {
        try
        {

            sqldb_query = "INSERT INTO MyTable (Name, Email, Age, City) VALUES ('" + sName + "','" + sEmail + "','" + iAge + "','" + sCity + "');";

            sqldb.ExecSQL(sqldb_query);
            sqldb_message = "Record saved";
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
     }

这是数据库类,下面是注​​册页面,

    Database sqldb;
    EditText txtName, txtAge, txtEmail, txtCity;

    Button imgAdd;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Registration);

        //Gets ImageButton object instances
        imgAdd = FindViewById<Button> (Resource.Id.imgAdd);

        //Gets EditText object instances
        txtAge = FindViewById<EditText> (Resource.Id.txtAge);
        txtEmail = FindViewById<EditText> (Resource.Id.txtEmail);
        txtName = FindViewById<EditText> (Resource.Id.txtName);
        txtCity = FindViewById<EditText> (Resource.Id.txtCity);

        imgAdd.Click += delegate {
                sqldb = new Database("details_db");
                sqldb.AddRecord (txtName.Text, txtEmail.Text, int.Parse (txtAge.Text), txtCity.Text);
            txtName.Text = txtAge.Text = txtEmail.Text = txtCity.Text = "";

                StartActivity(typeof(MainActivity));
                Finish();
            }

名称和电子邮件字段应显示在此处

        const string sqldb_name = "details_db";
        EditText name = FindViewById<EditText> (Resource.Id.name);
        EditText email = FindViewById<EditText> (Resource.Id.email);
        string sqldb_location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string sqldb_path = Path.Combine(sqldb_location, sqldb_name);
        bool sqldb_exists = File.Exists(sqldb_path);
        if (sqldb_exists) {

            string[] from = new string[] {"Name","Email"};
            int[] to = new int[] {

                Resource.Id.name,
                Resource.Id.email,
            };
        }

0 个答案:

没有答案