0

我在 vb.net 中不断收到“连接字符串未正确初始化”错误,我不知道如何修复它。我也在尝试自动生成数字。例如,在我的表中,一旦我点击添加新它应该按顺序生成下一个数字。

这是我的代码:

Imports System.Data
Imports System.Data.OleDb   
Imports System.Data.SqlClient



Public Class AddTemplate

    Private Property OleDbConnection As Object

    Private Property temp As Object
        Dim dc As New OleDbConnection
        Dim drd As OleDbDataReader
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        Dim i As Integer = 0


    Private Sub TemplateNameTextBox_TextChanged(sender As Object, e As EventArgs)
        Try

        Catch ex As Exception

        End Try
    End Sub


    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click

    End Sub


    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        ValueSourcesBindingSource.EndEdit()
        Me.Update()
    End Sub


    Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
        Me.Close()
    End Sub


    Private Function ValueSourcesDataTable() As Object
        Me.Update()
    End Function


    Private Sub ValueSourcesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)HandlesValueSourcesBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ValueSourcesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ValueTrackerDataSet)
    End Sub


    Private Sub AddTemplate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ValueTrackerDataSet.ValueSources' table. You can move, or remove it, as needed.
        Me.ValueSourcesTableAdapter.Fill(Me.ValueTrackerDataSet.ValueSources)
    End Sub


    Private Sub ValueSourcesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles ValueSourcesDataGridView.CellContentClick
    End Sub


    Public Sub AutoNumberNo()
        Dim myReader As SqlDataReader
        conn = GetConnect()
        conn.Open()

        Dim comm As SqlCommand = New SqlCommand(Sql, conn)
        myReader = comm.ExecuteReader
        Try
            If myReader.HasRows Then
                While myReader.Read()
                    temp = myReader.Item("ValueSourceID") + 1
                End While
            Else

            End If
            temp = 1
            End
            myReader.Close()
        Catch ex As Exception

        End Try
        conn.Close()
        ValueSourceIDTextBox.Text = String.Concat(temp) ' result will appear in textbox txtId


        'declare variables
        Dim randomvalue As New Random   'create random object
        Dim randomhold As Integer
        'generate random number
        For i As Integer = 0 To 9999
            randomhold = randomvalue.Next(1, 9999)
            ValueSourceIDTextBoxId.Text = randomhold & " " & DateTime.Now.Minute & "  " & DateTime.Now.Year
        Next
    End Sub


    Private Sub BindingNavigatorMoveNextItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveNextItem.Click

    End Sub


    Private Sub ValueSourceIDTextBox_TextChanged(sender As Object, e As EventArgs) Handles ValueSourceIDTextBox.TextChanged

    End Sub


    Private Function GetConnect() As Object
        Throw New NotImplementedException
    End Function


    Private Function Sql() As Object
        Throw New NotImplementedException
    End Function


    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click

    End Sub


    Private Sub ToolStripProgressBar1_Click(sender As Object, e As EventArgs)

    End Sub


    Private Sub BindingNavigatorMovePreviousItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMovePreviousItem.Click

    End Sub


    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles    BindingNavigatorAddNewItem.Click
        Dim conn As New OleDbConnection
        Dim connectionstring = ("Data Source=wal1sql1;Initial Catalog=ValueTracker;Integrated Security=True")

        conn.Open()


        Dim query As String = "Select IsNULL(Max(0+1), 0) ValueSourceID from ValueSourcesDataTable"
        Dim dr As SqlClient.SqlDataReader

        Dim cmd As New SqlCommand(query, SqlConnection)
        dr = cmd.ExecuteReader
        dr.Read()

        ValueSourcesDataTable.Text = dr("ValueSourceID").ToString
        conn.Close()
    End Sub


    Private Function ValueSourceIDTextBoxId() As Object
        Throw New NotImplementedException
    End Function


    Private Function SqlConnection() As Object
        Throw New NotImplementedException
    End Function

End Class
4

2 回答 2

2
Dim conn As New OleDbConnection
Dim connectionstring = ("Data Source=wal1sql1;Initial Catalog=ValueTracker;Integrated Security=True")

conn.Open()

应该:

Dim connectionstring = ("Data Source=wal1sql1;Initial Catalog=ValueTracker;Integrated Security=True")
Dim conn As New OleDbConnection(connectionstring)

conn.Open()
于 2013-07-01T14:28:20.010 回答
0

缺少初始化的明显错误的一部分,您应该决定是否需要 OleDbConnection 或 SqlConnection,

使用的语法对 SqlConnection 有效,对 OleDbConnection 无效。

 Dim connectionstring = "Data Source=wal1sql1;Initial Catalog=ValueTracker;Integrated Security=True"
 Dim query As String = "Select IsNULL(Max(0+1), 0) ValueSourceID from ValueSourcesDataTable"
 Using conn = New SqlConnection(connectionstring)
 Using cmd = New SqlCommand(query, conn)
     conn.Open()
     Using dr = cmd.ExecuteReader
         dr.Read()
         ValueSourcesDataTable.Text = dr("ValueSourceID").ToString
     End Using
 End Using
 End Using

一旦打开连接,就应该使用在同一命名空间中创建的对象(特别是在本例中为 System.Data.SqlClient)SqlCommand、SqlDataReader 等。您不能随意混合 OleDb 和 SqlClient。

sql查询也有点奇怪。你想用这种语法实现什么?

编辑
回应您在下面的评论。初始化是每个类提供的用于创建类的“活动”实例的过程。在此过程中,调用代码可以传递零个或多个参数,这些参数将由类代码在内部使用以提供初始工作状态。
在类 SqlConnection 的特定情况下,您使用语法instancename = new classname(parameters)初始化一个调用特殊“构造函数”方法的“实时”实例,其中参数是一个名为 connectionstring 的字符串,其中包含查找服务器和包含您的数据的数据库。初始化后,您可以开始调用类提供的方法,如 Open、Close 等...

于 2013-07-01T14:46:07.893 回答