• [转]C# 操作Sqlite


        public class BookDAL
        {
            public static bool CreateBook(Book book)
            {
                try
                {
                    SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;");
                    conn.Open();
                    SQLiteCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "INSERT INTO Book(ID, BookName, Price, Rowguid) VALUES(@ID1, @BookName1, @Price1, @Rowguid1)";
                    cmd.Parameters.Add(new SQLiteParameter("ID1", book.ID));
                    cmd.Parameters.Add(new SQLiteParameter("BookName1", book.BookName));
                    cmd.Parameters.Add(new SQLiteParameter("Price1", book.Price));
                    cmd.Parameters.Add(new SQLiteParameter("Rowguid1", book.Rowguid));
    
                    int i = cmd.ExecuteNonQuery();
                    return i == 1;
                }
                catch (SQLiteException se)
                {
                    MessageBox.Show(se.Message + " 
    
    " + se.Source + "
    
    " + se.StackTrace + "
    
    " + se.Data);
                    return false;
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace + "
    
    " + ae.Data);
                    return false;
                }
                catch (Exception ex)
                {
                    //Do any logging operation here if necessary
                    MessageBox.Show(ex.Message + "
    
    " + ex.Source + "
    
    " + ex.StackTrace + "
    
    " + ex.Data);
                    return false;
                }            
            }
    
            public static bool UpdateBookByID(Book book)
            {
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;"))
                    {
                        conn.Open();
                        SQLiteCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "update Book set BookName=@BookName1,Price=@Price1, Rowguid=@Rowguid1 where ID=@ID1;";
                        cmd.Parameters.Add(new SQLiteParameter("ID1", book.ID));
                        cmd.Parameters.Add(new SQLiteParameter("BookName1", book.BookName));
                        cmd.Parameters.Add(new SQLiteParameter("Price1", book.Price));
                        cmd.Parameters.Add(new SQLiteParameter("Rowguid1", book.Rowguid));
                        int i = cmd.ExecuteNonQuery();
                        return i == 1;
                    }
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace);
                    return false;
                }
                catch (Exception ex)
                {
                    //Do any logging operation here if necessary
                    MessageBox.Show(ex.Message);
                    return false;
                }
            }
    
            public static bool UpdateBookByGuid(Book book)
            {
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;"))
                    {
                        conn.Open();
                        SQLiteCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "update Book set ID=@ID1,BookName=@BookName1,Price=@Price1 where Rowguid=@Rowguid1;";
                        cmd.Parameters.Add(new SQLiteParameter("ID1", book.ID));
                        cmd.Parameters.Add(new SQLiteParameter("BookName1", book.BookName));
                        cmd.Parameters.Add(new SQLiteParameter("Price1", book.Price));
                        cmd.Parameters.Add(new SQLiteParameter("Rowguid1", book.Rowguid));
                        int i = cmd.ExecuteNonQuery();
                        return i == 1;
                    }
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace);
                    return false;
                }
                catch (Exception ex)
                {
                    //Do any logging operation here if necessary
                    MessageBox.Show(ex.Message);
                    return false;
                }
            }
    
            public static bool DeleteBook(int ID)
            {
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;"))
                    {
                        conn.Open();
                        SQLiteCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "delete from Book where ID=@ID;";
                        cmd.Parameters.Add(new SQLiteParameter("ID", ID));
                        int i = cmd.ExecuteNonQuery();
                        return i == 1;
                    }
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace);
                    return false;
                }
                catch (Exception ex)
                {
                    //Do any logging operation here if necessary
                    MessageBox.Show(ex.Message);
                    return false;
                }
            }
    
            public static Book GetBookByID(int ID)
            {
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;"))
                    {
                        conn.Open();
                        SQLiteCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "select * from Book where ID=@ID;";
                        cmd.Parameters.Add(new SQLiteParameter("ID", ID));
                        SQLiteDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            Book book = new Book();
                            book.ID = dr.GetInt32(0);
                            book.BookName = dr.GetString(1);
                            book.Price = dr.GetDecimal(2);
                            return book;
                        }
                        else
                            return null;
                    }
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace);
                    return null;
                }
                catch (Exception ex)
                {
                    //Do any logging operation here if necessary
                    throw new Exception(ex.Message);
                }
            }
    
            public static DataTable GetAllBook()
            {
                DataTable dt = new DataTable();
                try
                {
                    SQLiteConnection conn = new SQLiteConnection("Data Source=db/Books.sqlite;");
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(conn);
                    cmd.CommandText = "SELECT * FROM Book";
                    cmd.CommandType = CommandType.Text;
                    //Console.WriteLine(cmd.CommandText);
                    SQLiteDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dt.Load(dr);
                    }
                    else {
                        //throw new NullReferenceException("No Record Available.");
                    }
    
                    dr.Close();
                    conn.Close();
                                    
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message + " 
    
    " + ae.Source + "
    
    " + ae.StackTrace + "
    
    " + ae.Data);
                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                    MessageBox.Show(ex.Message + " 
    
    " + ex.Source + "
    
    " + ex.StackTrace + "
    
    " + ex.Data);
                }
    
                return dt;
            }
        }
        
  • 相关阅读:
    移植tslib库出现selected device is not a touchscreen I understand的解决方法
    2017- 韦东山视频学员成果精选(三)
    2017-韦东山视频学员成果精选(二)
    2017-韦东山视频学员成果精选(一)
    字符设备驱动另一种写法—mmap方法操作LED
    使用ubuntu16.04配置linux内核和busybox出现错误的解决方法总结
    100000个嵌入式学习者遇到的PING不通问题,我们使用这一个视频就解决了,牛!
    推荐使用集串口,SSH远程登录和FTP传输三合一工具MobaXterm
    外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
    Laravel 日志配置以及设置按日期记录日志
  • 原文地址:https://www.cnblogs.com/qvbrgw/p/4760792.html
Copyright © 2020-2023  润新知