如何从SQLite中的列获取最大值到WPF C# [英] How to get the max value from a column in SQLite to WPF C#

查看:77
本文介绍了如何从SQLite中的列获取最大值到WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!



我无法从我的SQLite数据库中的列中获取特定值到我的WPF C#应用程序。



Greetings!

I was having trouble in getting a specific value from a column in my SQLite Database to my WPF C# application.

using System;
using System.Windows;
using System.Windows.Input;

using System.Data.SQLite;
namespace IEEE_UESB_Database
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        string dbConnectionString = @"Data Source=ieeeuesbmembers.sqlite;Version=3";
        int memID;
        public Window2()
        {
            InitializeComponent();
        }

        private void cmdAddNew_Click(object sender, RoutedEventArgs e)
        {
            memID++;
            tbxMemID.Text = Convert.ToString(memID);
            tbxLastName.Text = "";
            tbxFirstName.Text = "";
            tbxMiddleName.Text = "";
            tbxNickName.Text = "";
            tbxStudentNumber.Text = "";
            cbxYearLevel.Text = "";
            tbxContactNumber.Text = "";
            tbxEmailAddress.Text = "";
            cbxCourse.Text = "";
            tbxAddress.Text = "";
            cbxCommittee.Text = "";
            cbxPosition.Text = "";
        }

        private void cmdDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
            //Open connection to database
            try
            {
                sqliteCon.Open();
                string Query = "insert into ieeeuesbmembers (memberID,lastName,firstName,middleName,nickName,studentNumber,yearLevel,contactNumber,emailAddress,course,address,committee,position) values ('" + this.tbxMemID.Text + "','" + this.tbxLastName.Text + "','" + this.tbxFirstName.Text + "','" + this.tbxMiddleName.Text + "','" + this.tbxNickName.Text + "','" + this.tbxStudentNumber.Text + "','" + this.cbxYearLevel.Text + "','" + this.tbxContactNumber.Text + "','" + this.tbxEmailAddress.Text + "','" + this.cbxCourse.Text + "','" + this.tbxAddress.Text + "','" + this.cbxCommittee.Text + "','" + this.cbxPosition.Text + "')";
                SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
                createCommand.ExecuteNonQuery();
                MessageBox.Show("Record successfully saved!");
                sqliteCon.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdLogout_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Thank you for using this software!");
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
            //Open connection to database
            try
            {
                sqliteCon.Open();
                SQLiteCommand createCommand = new SQLiteCommand(sqliteCon);
                SQLiteDataReader dataReader = createCommand.ExecuteReader();
                memID = Convert.ToInt32(dataReader["select max(memID) from ieeeuesbmembers"]);
                tbxMemID.Text = Convert.ToString(memID);
                sqliteCon.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}





我的问题是在Windows_Loaded事件中,每次打开应用程序时,我都希望tbxMemID始终具有在数据库上记录为具有自动增量功能的主键的最大值,这样每次我尝试添加一个时都不会重叠任何记录。



不断给出此错误信息。



My problem is on the Windows_Loaded event, everytime the application is opened, I wanted the tbxMemID to always have the max value that was recorded as a primary key with autoincrement function on the database, so that I won't overlap any records everytime I tried to add one.

It keeps giving this error message.

Value cannot be null.
Parameter: s





有人可以帮帮我吗?

谢谢!



Can someone help me out?
Thanks!

推荐答案

像这样修改你的SQLiteCommand

Modify your SQLiteCommand like this
SQLiteCommand createCommand = new SQLiteCommand("select max(memID) from ieeeuesbmembers",sqliteCon);



然后使用ExecuteReader。那么你将拥有dataReader对象中的数据。


and then use ExecuteReader. then you will have the data in your dataReader object.


在SQLite中你有一个很棒的过程....



只需用

In SQLite you have a nice proc for that....

Just replace your query with
SELECT seq FROM sqlite_sequence WHERE name='ieeeuesbmembers'





只要你有一个有效的连接,就可以使用



As long as you have a active connection, you can use

SELECT last_insert_rowid() FROM ieeeuesbmembers


按照原样保存代码,只需声明你的MemID:

var memID = dataReader [从ieeeuesbmembers选择max(memID);

if(memID == null)

{

memID = 0

}
keep your code the way it is, just declare your MemID like this:
var memID = dataReader["select max(memID) from ieeeuesbmembers"];
if (memID == null)
{
memID=0
}


这篇关于如何从SQLite中的列获取最大值到WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆