示例#1
0
        public IEnumerable <QA> GetQAs()
        {
            var qaList = new List <QA>();

            lock (locker)
            {
                var connection = new SqliteConnection("Data Source=" + path);
                connection.Open();
                using (var contents = connection.CreateCommand())
                {
                    contents.CommandText = "SELECT [Id], [Question], [Answer], [UserName] from [QuestionAnswers]";
                    var r = contents.ExecuteReader();
                    while (r.Read())
                    {
                        var qa = new QA();
                        qa.Id       = Convert.ToInt32(r["Id"]);
                        qa.Question = r["Question"].ToString();
                        qa.Answer   = r["Answer"].ToString();
                        qa.UserName = r["UserName"].ToString();
                        qaList.Add(qa);
                    }
                }
                connection.Close();
            }
            return(qaList);
        }
示例#2
0
        public int SaveItem(QA question)
        {
            int r;

            lock (locker)
            {
                if (question.Id != 0)
                {
                    connection = new SqliteConnection("Data Source=" + path);
                    connection.Open();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "UPDATE [QuestionAnswers] SET [Question] = ?, [Answer] = ?, [UserName] = ? WHERE [Id] = ?;";
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.Question
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.Answer
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.UserName
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.Int32)
                        {
                            Value = question.Id
                        });
                        r = command.ExecuteNonQuery();
                    }
                    connection.Close();
                    return(r);
                }
                else
                {
                    connection = new SqliteConnection("Data Source=" + path);
                    connection.Open();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "INSERT INTO [QuestionAnswers] ([Question], [Answer], [UserName]) VALUES (? ,?, ?)";
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.Question
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.Answer
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = question.UserName
                        });
                        r = command.ExecuteNonQuery();
                    }
                    connection.Close();
                    return(r);
                }
            }
        }