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

0

初期化が欠落しているという明らかなエラーの一部として、OleDbConnection が必要か SqlConnection が必要かを決定する必要があります。

使用されている構文は、OleDbConnection ではなく SqlConnection に対して有効です。

 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クエリは少し奇妙です。その構文で何を達成したいですか?

EDIT
以下のコメントに応じて。初期化は、クラスの「ライブ」インスタンスを作成するためにすべてのクラスが提供するプロセスです。このプロセスでは、呼び出し元のコードは、初期の作業状態を提供するためにクラス コードによって内部的に使用される 0 個以上のパラメーターを渡すことができます。クラス SqlConnection の特定のケースでは、構文instancename = new classname(parameters)
を使用して、特別な「コンストラクター」メソッドを呼び出す「ライブ」インスタンスを初期化します。ここで、パラメーターは、サーバーを見つけるために必要なすべての情報を含む connectionstring という文字列です。あなたのデータを含むデータベース。初期化の後、Open、Close などのクラスによって提供されるメソッドの呼び出しを開始できます...

于 2013-07-01T14:46:07.893 に答える