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

vb.net 中水晶报表怎么用代码(即动态)连接sql server 2005数据库

skwenl 发布于 2008-07-05 09:24, 2372 次点击
各位大虾:

    请问在vb 2005中 水晶报表怎么动态连接sql server 2005数据库呢? 能否给些代码?谢谢......
2 回复
#2
vpzq2008-07-05 12:27
Shared Function DBOperate(ByVal SQLString As String, ByRef Msg As String) As DataTable
        Try
            Dim CONN As String = "Provider = SQLOLEDB.1;data source=127.0.0.1\SQLEXPRESS;initial catalog=HYSYS;user id=sa;password=1234"

            Dim oleconn As New OleDb.OleDbConnection(CONN)
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SQLString, oleconn)
             = CommandType.Text
            Dim sTokens() As String
            sTokens = SQLString.Split(" ") '.split去掉中间的空格
            If Strings.InStr("INSERT,DELETE,UPDATE", sTokens(0).ToUpper) Then
                If oleconn.State <> ConnectionState.Open Then
                    oleconn.Open()  '打开数据库连接
                End If
                cmd.ExecuteNonQuery()  '执行SQL语句
                If oleconn.State <> ConnectionState.Closed Then
                    oleconn.Close() '关闭数据库连接
                End If
                If sTokens(0).ToUpper = "INSERT" Then
                    Msg = "插入记录成功"
                End If
                If sTokens(0).ToUpper = "DELETE" Then
                    Msg = "删除记录成功"
                End If
                If sTokens(0).ToUpper = "UPDATE" Then
                    Msg = "更新记录成功"
                End If
                Return Nothing
            Else
                Dim ObjectdsDataSet As New DataSet()
                Dim adapter As New OleDb.OleDbDataAdapter()
                adapter.TableMappings.Add("Table", "TEMP")
                adapter.SelectCommand = cmd
                If oleconn.State <> ConnectionState.Open Then
                    oleconn.Open()  '打开数据库连接
                End If
                cmd.ExecuteNonQuery()  '执行SQL语句
                If oleconn.State <> ConnectionState.Closed Then
                    oleconn.Close() '关闭数据库连接
                End If
                adapter.Fill(ObjectdsDataSet) '填充数据集
                Return ObjectdsDataSet.Tables("TEMP")
            End If
        Catch

            MsgBox(Err.Description)
        End Try
        Return Nothing
    End Function
#3
skwenl2008-07-05 15:20
谢谢二楼的回复,呵呵,我是想明白  水晶报表  动态连接  sql server 2005数据库  的代码.........
1