C#操作Sqlite数据库:创建数据库,创建表格,删增改查

创建数据库:

/// 
/// 新建数据库文件
/// 
/// 数据库文件路径及名称
/// 新建成功,返回true,否则返回false
static public Boolean NewDbFile(string m_FilePath)
{
  try
 {
     SQLiteConnection.CreateFile(m_FilePath);
    return true;
 }
  catch (Exception ex)
 {
    throw new Exception("新建数据库文件" + m_FilePath + "失败:" + ex.Message);
 }
}

eg:NewDbFile(“D:\\XXX.db”);
注意:在创建到D 盘时候可能会出现拒绝访问的情况,这种情况可能是权限不够,可以换个路径进行创建

创建表格:

/// 
/// 创建表
/// 
/// 指定数据库文件
/// 表名称
static public void NewTable(string dbPath, string tableName)
{

        SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
        if (sqliteConn.State != System.Data.ConnectionState.Open)
        {
            sqliteConn.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = sqliteConn;
            cmd.CommandText = "CREATE TABLE " + tableName + "(时间 Text,数据 Text, 状态 Text)";
            cmd.ExecuteNonQuery();
         }
     sqliteConn.Close(); 
}

连接数据库并查询显示:

 public void SQLiteGetInformationToShow(object sender, EventArgs e )//获取数据库的信息并显示
        {
            SQLiteDataAdapter m_Adapter = new SQLiteDataAdapter("select * from " + tableName, m_SqlConnection);
            dt = new DataTable();

            m_Adapter.Fill(dt);
            ListViewAllDisplay(dt, EventArgs.Empty);//写一个事件,在主窗口类中完善,使得datatable转换listview并填充到listview
        }

Form:

private void M_Manager_OnListViewAllDisplay(object sender,EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(M_Manager_OnListViewAllDisplay), sender, e);
                return;
            }

            DataTable dt = (DataTable)sender;
            tool.dataTableToListview(this.listViewDisplay, dt);//该转换函数请参考我的第一篇文章
        }

增加消息:

 public void Add(string[] keys, string[] values)
        {
            //INSERT INTO TableName(时间,数据,状态) VALUES('时间','数据','状态');
            string keys_string = "(" + keys[0];
            string value_string = "('" + values[0]+"'";

            for (int i = 1; i < keys.Length; i++)
            {
                keys_string += "," + keys[i];
            }
            for (int i = 1; i < values.Length; i++)
            {
                value_string += ",'" + values[i]+"'";
            }
            keys_string += ")";
            value_string += ")";
            string sql = string.Format("INSERT INTO "+TableName+" {0} VALUES {1}", keys_string, value_string);
            m_Cmd = new SQLiteCommand(sql, m_SqlConnection);
            int rows = m_Cmd.ExecuteNonQuery();
         
            MessageBox.Show("添加成功" + rows + "行");
        }

修改消息:

 public void Update(string[] keys, string[] values,string[] oldvalues) //修改信息
        {
            //UPDATE TableName SET 时间 = '时间', 数据 = '数据', 状态 = '状态' WHERE 时间 = '时间' AND 数据 = '数据' AND 状态 = '状态';
            string values_string = keys[0]+"='"+values[0]+"'";
            string oldvalues_string = keys[0] + "='" + oldvalues[0]+"'";
            for (int i = 1; i < values.Length; i++)
            {
                values_string += ","+ keys[i] + "=" + values[i];
            }
            for (int i = 1; i < oldvalues.Length; i++)
            {
                oldvalues_string += " And " + keys[i] + "='" + values[i]+"'";
            }
            string sql = string.Format("UPDATE "+TableName+" SET {0} WHERE {1}; ",values_string, oldvalues_string);
            m_Cmd = new SQLiteCommand(sql, m_SqlConnection);
            int rows = m_Cmd.ExecuteNonQuery();
           
            MessageBox.Show("修改成功!" + rows + "行");
        }

删除消息:

 public void Delete(string[] keys,string[] values)//删除信息
        {
            //DELETE FROM TableName WHERE 时间 = '时间' AND 数据 = '数据' AND 状态 = '状态';
            string values_string = keys[0] + "='" + values[0] + "'";
            for (int i = 1; i < values.Length; i++)
            {
                values_string += "And" + keys[i] + "='" + values[i] + "'";
            }
            string sql = string.Format("DELETE FROM "+TableName+" WHERE {0} ", values_string);
            m_Cmd = new SQLiteCommand(sql, m_SqlConnection);
            int rows = m_Cmd.ExecuteNonQuery();

            MessageBox.Show("删除成功!" + rows + "行");
        }

最后补充关于VS2015的问题:

笔者最近遇到了VS2015崩溃,显示菜单初始化出错,需要打开VS命令窗口输入:devenv.exe/resetuserdata
但是输入以后还是打不开程序,笔者试着卸载重新安装2015,还是一样的问题
最后用2013了,如果您有好的解决办法,请在评论区留下高见,谢谢各位。

你可能感兴趣的:(C#操作Sqlite数据库:创建数据库,创建表格,删增改查)