Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

WPF C# reading data from SQLite using Dapper Query

I am trying to read a SQLite tables using Dapper Query with a model of the data. I have similar code in another application which work perfectly and have copied the code. However, although it reads the correct number of rows the data is 0 (for integers) and null (for strings). I have created a very simple version of the problem shown below. Why can I not retrieve the data correctly?

  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data;
  using System.Data.SQLite;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.Text.RegularExpressions;
  using Dapper;

  namespace TestLoadData
     public partial class MainWindow : Window
        public MainWindow()

        private void Load_Click(object sender, RoutedEventArgs e)
           string cmdSQL = @"SELECT * FROM AdrsSets;";
           List<AdrsSetModel> oldAdrsSets = new List<AdrsSetModel>();
           Dictionary<string, object> parameters = new Dictionary<string, object>();
           DynamicParameters p = new DynamicParameters();
           parameters.ToList().ForEach(x => p.Add(x.Key, x.Value));
           string dBFilePath = "C:\Users\ajh\Desktop\AdrsBook1.dB3";
           using (IDbConnection cnn = new SQLiteConnection(GetConnectionString(dBFilePath)))
              var rows = (List<AdrsSetModel>)cnn.Query<AdrsSetModel>(cmdSQL, p);

        internal static string GetConnectionString(string dbFilepath)
           string cn = "";
           string cntnStr = ConfigurationManager.ConnectionStrings["SQLiteConnect"].ConnectionString;
           Regex re = new Regex("dBPATHNAME");
           cn = re.Replace(cntnStr, dbFilepath);
           return cn;
     public class AdrsSetModel
           public int AdrsSetKey { get; set; }
           public string SetName { get; set; }

the table structure is: CREATE TABLE AdrsSets ("AdrsSet Key" INTEGER PRIMARY KEY, "Set Name" TEXT DEFAULT "")

The table data is:

Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share