Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
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()
        {
           InitializeComponent();
        }

        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

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...