sqliteHelp类似sqlHelp

    class C_SQLite
    {
        /// <summary>
        /// 获得连接对象
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetSQLiteConnection()
        {
            return new SQLiteConnection(@"Data Source=data.db");
        }
       
        /// <summary>
        /// 修改数据表记录
        /// </summary>
        /// <returns></returns>
        public bool UpdateTable(DataTable srcTable, string tableName)
        {
            bool isok = false;
            try
            {
                SQLiteCommand command = new SQLiteCommand();
                command.CommandText = "SELECT * FROM " + tableName;
                SQLiteDataAdapter SQLiteDA = new SQLiteDataAdapter(command);
                SQLiteCommandBuilder SQLiteCB = new SQLiteCommandBuilder(SQLiteDA);
                SQLiteDA.InsertCommand = SQLiteCB.GetInsertCommand();
                SQLiteDA.DeleteCommand = SQLiteCB.GetDeleteCommand();
                SQLiteDA.UpdateCommand = SQLiteCB.GetUpdateCommand();
                SQLiteDA.Update(srcTable);
               
                isok = true;
            }
            catch
            { ;}
            return isok;
        }
        /// <summary>
        /// 读取返回数据表
        /// </summary>       
        public static DataTable ExecuteDataTable(string SQLStr)
        {
            DataTable datatable = new DataTable("mytabe");
            SQLiteCommand command = new SQLiteCommand();
            using (SQLiteConnection conn = GetSQLiteConnection())
            {
                if (conn.State != ConnectionState.Open)               conn.Open();               
                command.Connection = conn;
                command.CommandText = SQLStr;
                SQLiteDataAdapter da = new SQLiteDataAdapter(command);              
                da.Fill(datatable);
                conn.Close();
            }
            return datatable;
        }

        /// <summary>
        /// 返回受影响的行数
        /// </summary>      
        public static int UpdateData(string SQLStr)
        {
            SQLiteCommand command = new SQLiteCommand();
            using (SQLiteConnection conn = GetSQLiteConnection())
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                command.Connection = conn;
                command.CommandText = SQLStr;
                command.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
        }
        //绑定数据表格
        public static void BindGrid(DataGridView mydatagrid, string SQLStr)
        {
            mydatagrid.DataSource = ExecuteDataTable(SQLStr);
        }
        //绑定数据下拉式文本框
        public static void BindCbox(ComboBox cbox, string SQLStr, string DspyFld)
        {
            cbox.DataSource = ExecuteDataTable(SQLStr);
            cbox.DisplayMember = DspyFld;
        }
        //设定数据表格的列宽
        public static void SetGridWidth(DataGridView mydatagrid, int[] width)
        {
            for (int i = 0; i < width.Length; i++)
                mydatagrid.Columns[i].Width = width[i];
        }
        //返回得到data数据
        public static string[,] GetData(string SQLStr)
        {
            DataTable datatable = ExecuteDataTable(SQLStr);
            string[,] reData = new string[datatable.Rows.Count, datatable.Columns.Count];
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                    reData[i, j] = datatable.Rows[i][j].ToString();
                }
            }
            return reData;
        }

posted @ 2011-06-22 10:30  咸鱼公子  Views(1265)  Comments(0Edit  收藏  举报