• winform程序如何读取程序内DB文件


    首先程序里的DB文件,我使用Navcat创建一个DB文件(类型是Sqlite).然后再添加到程序里面。读取程序里的数据库首先要先读取DB文件的数据路径,然后用在使用SQL语句进行的读取和存储数据(记住使用sqlitehelper类,百度上就有)

    首先选取路径,我使用的方法:string Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\DB.db3";

    连接数据库的方法

     string connString="Date Source="+Path;

    SQLiteConnection conn = new SQLiteConnection(connString);//连接DB

    我这里是是将姓名和ID保存在数据库中,只保留一行新数据行,若是出现新的ID则将覆盖上一个ID。

    举个例子将数据保存在数据库中:

    public static SQLiteConnection  connection()//连接数据库

    {

    string Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\DB.db3";

    if(file.exists(path))

    {

     string connString="Date Source="+Path;

    SQLiteConnection conn = new SQLiteConnection(connString);//连接DB

    return conn;

    }

    else

    {

    return null;

    }

    }

    public static void saveData(string data1,string data2)//存数据

    {

    string cmdText = string.Empty;
    if (!string.IsNullOrEmpty(FeedType) || !string.IsNullOrEmpty(IPAddress))
     {
    str = string.IsNullOrEmpty(str) ? "" : "ConnectStr='" + str + "'";
    feedType = string.IsNullOrEmpty(feedType) ? "" : "FeedType='" + feedType + "'";
    cmdText = string.Format("update CSSetting set {0},{1}", str, feedType);//为什么没有用WHERE语句,因为我只用一行当有新数据时我直接更新数据,当空的时候我才给她赋值
    }
    else
    {
    cmdText = string.Format("insert into CSSetting (ConnectStr,FeedType) Values('{0}','{1}')", str, feedType);
    }
    return SqliteHelper.ExecuteNonQuery(cmdText);

    }

    }

    public static void getData()

    {

    try
    {
    string cmdText = "select * from CSSetting";
    DbDataReader reader = SqliteHelper.ExecuteReader(cmdText);
    //var reader = SqliteHelper.ExecuteReader(cmdText);
    //SQLiteDataReader reader = SqliteHelper.ExecuteReader(cmdText);
    List<string> Config = new List<string>();
    if (reader != null)
    {
    while (reader.Read())//读取数据

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SQLite;
    using System.Data;
    using System.IO;
    using System.Reflection;
    
    namespace Infrastructure
    {
        public static class SqliteHelper//这是我使用的sqliterhelp,百度就有
        {
            private static SQLiteConnection conn = null;
            private static SQLiteCommand cmd = null;
    
            public static SQLiteConnection Connetion
            {
                get { return conn; }
                set { conn = value; }
            }
    
            //获得连接对象
            public static SQLiteConnection GetSQLiteConnection()
            {
                string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\DB.db3";
                try
                {
                    string connString = "Data Source=" + appPath;
                    SQLiteConnection conn = new SQLiteConnection(connString);
                    return conn;
                }
                catch (System.Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    return null;
                }
            }
    
            //命令准备
            private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p)
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
    
                cmd.Parameters.Clear();
                cmd.Connection = conn;
                cmd.CommandText = cmdText;
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 30;
                if (p != null)
                {
                    foreach (object parm in p)
                        cmd.Parameters.AddWithValue(string.Empty, parm);
                }
            }
    
            //为批量操作设置长连接的对象
            public static void GetBatchConn()
            {
                string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                try
                {
                    string connString = "Data Source=" + appPath + "\RTPS_PT_DB1.db3";
                    conn = new SQLiteConnection(connString);
                    conn.Open();
                    cmd = new SQLiteCommand();
                }
                catch (System.Exception ex)
                {
                    conn = null;
                    cmd = null;
                }
            }
    
            //关闭用于事务的连接对象
            public static void CloseBatchConn()
            {
                try
                {
                    if (conn != null && conn.State != ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                        if (cmd != null)
                        {
                            cmd.Dispose();
                        }
                    }
                }
                catch (System.Exception ex)
                { }
                finally
                {
                    conn = null;
                    cmd = null;
                }
            }
    
            //获取命令执行的结果数据集
            public static DataSet ExecuteDataset(string cmdText, string tableName, params object[] p)
            {
                try
                {
                    DataSet ds = new DataSet();
                    SQLiteCommand command = new SQLiteCommand();
                    using (SQLiteConnection connection = GetSQLiteConnection())
                    {
                        PrepareCommand(command, connection, cmdText, p);
                        SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                        adapter.Fill(ds, tableName);
                        
                    }
                    return ds;
                }
    
                catch (System.Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                    return null;
                }
            }
    
            //获取查询所得行
            public static DataRow ExecuteDataRow(string cmdText, string tableName, params object[] p)
            {
                DataSet ds = ExecuteDataset(cmdText, tableName, p);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0];
                return null;
            }
    
            //执行sql语句,返回受影响的行数
            public static int ExecuteNonQuery(string cmdText, params object[] p)
            {
                try
                {
                    SQLiteCommand command = new SQLiteCommand();
                    using (SQLiteConnection connection = GetSQLiteConnection())
                    {
                        PrepareCommand(command, connection, cmdText, p);
                        return command.ExecuteNonQuery();
                    }
                }
                catch (System.Exception ex)
                {
                    return -1;
                }
            }
    
            //执行sql语句(用于批量操作)
            public static int BatchExecuteNonQuery(string cmdText, params object[] p)
            {
                try
                {
                    PrepareCommand(cmd, conn, cmdText, p);
                    return cmd.ExecuteNonQuery();
                }
                catch (System.Exception ex)
                {
                    return -1;
                }
            }
    
            //返回SqlDataReader对象
            public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p)
            {
                SQLiteCommand command = new SQLiteCommand();
                SQLiteConnection connection = GetSQLiteConnection();
                try
                {
                    PrepareCommand(command, connection, cmdText, p);
                    SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    return reader;
                }
                catch (Exception ex)
                {
                    connection.Close();
                    return null;
                }
            }
    
            //返回结果集中的第一行第一列,忽略其他行或列
            public static object ExecuteScalar(string cmdText, params object[] p)
            {
                try
                {
                    SQLiteCommand cmd = new SQLiteCommand();
                    using (SQLiteConnection connection = GetSQLiteConnection())
                    {
                        PrepareCommand(cmd, connection, cmdText, p);
                        return cmd.ExecuteScalar();
                    }
                }
                catch (System.Exception ex)
                {
                    return null;
                }
            }
    
            //分页
            public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params object[] p)
            {
                if (recordCount < 0)
                    recordCount = int.Parse(ExecuteScalar(countText, p).ToString());
                DataSet ds = new DataSet();
                SQLiteCommand command = new SQLiteCommand();
                using (SQLiteConnection connection = GetSQLiteConnection())
                {
                    PrepareCommand(command, connection, cmdText, p);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                    da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result");
                }
                return ds;
            }
        }
    }


    {
    Config.Add(reader.GetString(0));
    Config.Add(reader.GetString(1));
    }
    }
    if (Config != null && Config.Count > 1)
    {
    IPAddress = Config[0];
    FeedType = Config[1];
    }
    }
    catch
    {

    }

    }

  • 相关阅读:
    element-ui实现表格el-table展开行
    jQuery请求本地JSON文件,在谷歌浏览器运行时报跨域错误
    jQuery使用serialize获取form表单数据,中文乱码问题
    Element-ui局部添加loading效果
    Vue中this.$set的使用
    项目管理【81】 | 项目立项管理
    项目管理【80】 | 知识产权与标准规范-标准规范
    项目管理【79】 | 知识产权与标准规范-政府采购法法
    项目管理【78】 | 知识产权与标准规范-招投标法
    项目管理【77】 | 知识产权与标准规范-合同法
  • 原文地址:https://www.cnblogs.com/xyyshishuaige/p/8661449.html
Copyright © 2020-2023  润新知