简体   繁体   English

如何在vb.net中修复错误“连接字符串未正确初始化”?

[英]How do I fix the error 'Connection string has not properly been initialized' in vb.net?

I keep getting 'connection string has not properly been initialized' error in vb.net and I don't know how to fix it. 我一直在vb.net中得到'连接字符串未正确初始化'错误,我不知道如何修复它。 I also am trying to auto-generate numbers. 我也在尝试自动生成数字。 For example, in my table once I hit add new it should generate the next number in order. 例如,在我的表中,一旦我点击添加新的,它应该按顺序生成下一个数字。

Here is my code: 这是我的代码:

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
Dim conn As New OleDbConnection
Dim connectionstring = ("Data Source=wal1sql1;Initial Catalog=ValueTracker;Integrated Security=True")

conn.Open()

should be: 应该:

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

conn.Open()

A part from the obvious error of the missing initialization, you should decide if you want an OleDbConnection or a SqlConnection, 作为缺少初始化的明显错误的一部分,您应该决定是否需要OleDbConnection或SqlConnection,

The syntax used is valid for a SqlConnection not for an OleDbConnection. 使用的语法对于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

Once opened the connection should be used from objects created in the same namespace (specifically in this case System.Data.SqlClient) SqlCommand, SqlDataReader etc.... You can't freely mix OleDb and SqlClient. 一旦打开,连接应该使用在同一命名空间中创建的对象(特别是在本例中为System.Data.SqlClient)SqlCommand,SqlDataReader等....你不能自由地混合OleDb和SqlClient。

Also the sql query is bit weird. sql查询也有点奇怪。 What do you want to achieve with that syntax? 你想用这种语法实现什么?

EDIT 编辑
In response at your comment below. 在下面的评论作出回应。 The initialization is a process that every class provides to create a 'live' instance of the class. 初始化是每个类提供的过程,用于创建类的“实时”实例。 In this process the calling code could pass zero or more parameters that will be used internally by the class code to provide an initial working state. 在此过程中,调用代码可以传递零个或多个参数,这些参数将由类代码在内部使用,以提供初始工作状态。
In the specific case of the class SqlConnection you initialize a 'live' instance calling the special 'constructor' method using the syntax instancename = new classname(parameters) where parameters is a string called connectionstring with all the information required to find the server and the database containing your data. 在类SqlConnection的特定情况下,您使用语法instancename = new classname(parameters)初始化一个调用特殊“构造函数”方法的“实时”实例,其中参数是一个名为connectionstring的字符串,其中包含查找服务器所需的所有信息以及包含数据的数据库。 After the initialization you could start to call the methods provided by the class like Open, Close, etc... 初始化后,您可以开始调用类提供的方法,如Open,Close等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接字符串属性尚未初始化 vb.net - The Connection String Property Has Not Been Initialized vb.net VB.NET 2010:错误“ ErrorExecuteReader:连接属性尚未初始化” - VB.NET 2010: Error “ErrorExecuteReader: Connection property has not been initialized” vb.net错误->填充:SelectCommand.Connection属性尚未初始化 - vb.net error -->Fill: SelectCommand.Connection property has not been initialized VB.NET错误:“ ConnectionString属性尚未初始化。” - VB.NET Error: “The ConnectionString property has not been initialized.” “VBS中的ConnectionString属性尚未初始化”错误 - “The ConnectionString property has not been initialized” error in VB.NET VB.NET的ConnectionString属性尚未初始化错误 - The ConnectionString property has not been initialized error with VB.NET VB.NET 中的“ConnectionString 属性尚未初始化”错误 - “The ConnectionString property has not been initialized” error in VB.NET VB.NET SQL Server插入 - ExecuteNonQuery:尚未初始化Connection属性 - VB.NET SQL Server Insert - ExecuteNonQuery: Connection property has not been initialized ExecuteReader:连接属性尚未初始化。 在vb.net 2010中 - ExecuteReader: Connection property has not been initialized. in vb.net 2010 VB.NET SQL Server Select Count(*) - ExecuteNonQuery:连接属性尚未初始化 - VB.NET SQL Server Select Count(*) - ExecuteNonQuery: Connection property has not been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM