欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vb.net利用system.data.sqlite.dll实现sqlite数据库编程示例

程序员文章站 2022-11-28 10:43:23
在项目开发中充分体会到了一个精简数据存储模块的重要性及实用性;在综合了三年开发经验的基础上,向各位推介一下sqlite,希望更多的程序猿支持开源精神。 imports system.data.sql...

在项目开发中充分体会到了一个精简数据存储模块的重要性及实用性;在综合了三年开发经验的基础上,向各位推介一下sqlite,希望更多的程序猿支持开源精神。
imports system.data.sqlite
public class form1
    dim conn as sqliteconnection
    private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
        if system.io.file.exists("test.db3") = true then
            system.io.file.delete("test.db3")
        end if
        sqliteconnection.createfile("test.db3")
        conn = new sqliteconnection("data source=test.db3;pooling=true;failifmissing=false")
        if conn.state <> connectionstate.open then
            conn.open()
            msgbox("打开成功!")
        end if

        dim cmd as new sqlitecommand
        cmd.connection = conn
        cmd.commandtext = "create table test (id integer primary key,testname varchar(500),testtime datetime,operator varchar(100))"
        dim result as integer = cmd.executenonquery()
        if result = 0 then
            msgbox("成功")
        else
            msgbox("失败")
        end if

        cmd = conn.createcommand()
        cmd.commandtext = "insert into test(testname,testtime,operator)values(@name,@testtime,@operator)"
        cmd.parameters.add("@name", data.dbtype.string).value = "动静"
        cmd.parameters.add("@testtime", data.dbtype.datetime).value = now()
        cmd.parameters.add("@operator", data.dbtype.string).value = "peer"
        result = cmd.executenonquery()
        if result <> 0 then
            msgbox("插入成功")
        end if
        selectshowinfo()

        '
        cmd = conn.createcommand()
        cmd.commandtext = "update  test set testname='动静1'"
        result = cmd.executenonquery()
        if result <> 0 then
            msgbox("更新成功")
        end if    
        selectshowinfo()

        '

        cmd = conn.createcommand()
        cmd.commandtext = "delete from test"
        result = cmd.executenonquery()
        if result <> 0 then
            msgbox("删除成功")
        end if
        selectshowinfo()

        cmd.dispose()

        if conn.state = connectionstate.open then
            conn.close()
        end if
    end sub

    public sub selectshowinfo()
        dim sa as new sqlitedataadapter("select * from test", conn)
        dim ds as new system.data.dataset
        sa.fill(ds, "test")
        dim mytable as new system.data.datatable
        mytable = ds.tables("test")
        me.datagridview1.datasource = mytable
        me.datagridview1.refresh()
    end sub
end class


作者:angxiao