zoukankan      html  css  js  c++  java
  • VS2017 C# winform 项目使用 sqlite (二)

    (一)连接数据库&查询数据库,(例子:用户登录窗)

            private void BtnLogin_Click(object sender, EventArgs e)
            {
                //登录检测
                try
                {
                    string dbPath = "Data Source=" + Directory.GetCurrentDirectory() + @"dataorders.db;Version=3;Password=07762828216;";
                    using ( SQLiteConnection conn = new SQLiteConnection(dbPath)) {
                        //查询是否有相应的数据
                        string uname = TxbUsername.Text.Trim();
                        string upwd = TxbPassword.Text.Trim();
                        string sql_select = "select count(0) from users where username =@uname and password =@upwd ";
                        //连接数据库
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }
                        SQLiteCommand cmd = new SQLiteCommand(sql_select, conn);
                        //cmd.Parameters.Add(new SQLiteParameter("@uname", uname));
                        //cmd.Parameters.Add(new SQLiteParameter("@upwd", upwd));
                        SQLiteParameter[] paras = new SQLiteParameter[] { new SQLiteParameter("@uname", uname), new SQLiteParameter("@upwd", upwd) };
                        cmd.Parameters.AddRange(paras);
                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.Read())
                        {
                            int result = Convert.ToInt16(reader.GetValue(0));
                            if (result == 0)
                            {
                                MessageBox.Show("用户名或者密码错误,请重新输入!"); 
                            }
                            else if(result == 1)
                            {
                                MessageBox.Show("登录成功!");
                            }
                            else
                            {
                                MessageBox.Show("程序出错,请不要输入奇怪的的内容!");
                                //退出软件
                                System.Environment.Exit(0);
                            }
                        }
                        //关闭数据库连接
                        reader.Close();
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                   MessageBox.Show("连接数据库失败:" + ex.Message);
                }
            }
  • 相关阅读:
    机器学习-好讲解
    caffe-BN层
    所有子文件夹中图片个数matlab代码实现
    17.5.11 自己领悟
    ubuntu16.04初始安装+无gpu+caffe+python2+opencv2+matlab2016+tensorflow
    No module named caffe
    Ubuntu14.04_64位使用过程
    Ubuntu14 sudo apt-get install apt-show-versions出错
    Active MQ 传输 ObjectMessage 异常
    spring 在静态工具类中使用注解注入bean
  • 原文地址:https://www.cnblogs.com/nb08611033/p/8806938.html
Copyright © 2011-2022 走看看