注册 登录
编程论坛 VB.NET论坛

VB2005數據庫及SQL語法

jxyga111 发布于 2008-10-17 11:16, 2205 次点击
我現在是自學VB2005但是發現,很多方式都與VB6.0不一樣,像連接數據庫,用SQL語法,都不一樣
望各位給幾個示例
謝謝
4 回复
#2
winorange2008-10-17 11:21
光连上我会了,可是我完全不会操作.
#3
ynyxsky2008-10-17 14:14
vb2005使用来操作数据库,操作方面要比vb6强大,sql语法上和vb6是一样的使用.
#4
ynyxsky2008-10-17 14:29
这是我一个程序里的模块,你试试吧

Imports System.Data
Imports System.Data.SqlClient


Module modSQLData
   Public ConnStr As String = "Persist Security Info=False;User ID=XXX;Password=XXX;Initial Catalog=XXX;Data Source=XXX;" '连接字符串
    Dim Conn As SqlConnection 'conn对象



#Region "填充返回dataset"

    Public Function FillDataSet(ByVal SQLStr As String) As DataSet
        Try
            Conn = New SqlConnection(ConnStr)
            Conn.Open()
            Dim da As SqlDataAdapter = New SqlDataAdapter(SQLStr, Conn)
            Dim ds As DataSet = New DataSet
            da.Fill(ds)
            Return ds
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "提示")
        Finally
            Conn.Close()
        End Try
    End Function

#End Region




#Region "执行SQL语句"
    Public Function EXESQLCommand(ByVal SqlStr As String) As Boolean
        Try
            Conn = New SqlConnection(ConnStr)
            Conn.Open()
            Dim SqlCmd As SqlCommand
            SqlCmd = New SqlCommand(SqlStr, Conn)
            SqlCmd.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "提示")
        Finally
            Conn.Close()
        End Try
    End Function
#End Region



#Region "执行统计"
    Public Function EXESQLScalar(ByVal SqlStr As String) As Single
        Try
            Conn = New SqlConnection(ConnStr)
            Conn.Open()
            Dim SqlSc As SqlCommand
            Dim SqlCount As Single
            SqlSc = New SqlCommand(SqlStr, Conn)
            SqlCount = SqlSc.ExecuteScalar()
            EXESQLScalar = SqlCount
        Catch ex As Exception
            'MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "提示")
            EXESQLScalar = 0
        Finally
            Conn.Close()
        End Try
    End Function
#End Region

如果要从数据库里取出数据显示在Datagridview上,那么如此使用:
dim SQLStr as string="select * from xxx"
DataGridView1.DataSource = FillDataSet(SQLStr).Tables(0)

如果要执行命令,如新增,删除,更新
dim SQLStr as string="inner........delete........update........"
EXESQLCommand(sqlstr)
#5
swjtu_li2008-10-17 17:29
你好
我也在学,老师让我做 库存系统;希望得到大家的帮助.我做的是餐饮系统中库存部分,包括订单管理,用户管理,出入库管理,耗损管理,统计查询等部分.
1