2016-03-23 1 views
0

一連のダウンロードコネクタ& &をダウンロードした後、最終的にxamppをVB.Netに接続することができました。 SQL insert and select statementsが必要な各フォームに接続文字列を入力するたびに非常に時間がかかる/面倒ですが、Class1.vbを作成し、そこにすべての接続文字列をxamppに配置するという考え方を思いつきました。だから私は、私に新しいフォームを作成し、そのたびに、私は私が述べてきたようにDim conn As New MySqlConnectionにアクセスできるように、私がしなければならないすべてはimportのClass1.vbに、各フォームにあるが何とか私はMYSQLに接続します、しかしClass1.vbClass1.vbの "all"宣言された変数にvb.netのさまざまな形式のソリューションにアクセス

に宣言されていますdbtableの名前を持つ変数を含むほとんどの変数にアクセスすることはできません。 私のClass1.vb

Public Class Class1 
Dim conn As New MySqlConnection 
Dim command As MySqlCommand 
Dim reader As MySqlDataReader 

Public Sub mysqlConnect() 

If Not conn Is Nothing Then conn.Close() 
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword) 
    Try 
     conn.Open() 

     MessageBox.Show("Connected!") 
    Catch x As Exception 
     MsgBox(x.Message) 
    End Try 
    conn.Close() 
end sub 
end class 

インサイドForm1.vbを

Public Class Form1 
Dim newCon as New Class1 


Private Sub Form2_Load 
newCon.mysqlConnect() 
'Note: mysqlConnect() is a function declared in Class1.vb 


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Try 
     conn.Open() 
    Catch x As Exception 
    End Try 
    Dim command As New MySqlCommand(String.Format("INSERT STATEMENT")) 
    command.ExecuteNonQuery() 
    conn.Close() 
    End Sub 

内だが、私はのButton1 Clickで示す変数が動作していないものをお見せしましょう。これは、あたかも前述のように、私がDBと完全に接続していることを意味するMsgbox( "Connected")が機能したのですが、Public Sub Connect() の機能を読み取れないかのようです。

いいえ、私の友人ですか?

ご意見ありがとうございます。前もって感謝します。

+0

私はクラス1から接続を見ることができますか? mysqlConnect() – Claudius

+0

"Connected!"と表示された直後にconnが閉じられます。また、conn1はClass1の変数ですが、Button1の中にconnという他の変数を使用しているようです。 –

+0

ああ!申し訳ありませんが、mysqlConnectをnewConのconnect()にミスしました。それは 'newCon.mysqlConnect()'でなければなりません。私はそれを編集します。ありがとうございます。 @Claudius – MoonPrincess

答えて

0

2つの一般的な解決策は次のとおりです。 1)Class1に「工場出荷時の方法」を変更して、接続を開放します。 2)あなたのクラスの内部で

例1の接続をラップ:

Public Class Class1 
Dim conn As New MySqlConnection 
Dim command As MySqlCommand 
Dim reader As MySqlDataReader 


Public Shared Function mysqlConnect() AS MySqlConnection 

    If Not conn Is Nothing Then conn.Close() 
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword') 
    Try 
     conn.Open() 

     ''MessageBox.Show("Connected!") 
    Catch x As Exception 
     MsgBox(x.Message) 
    End Try 
    ''conn.Close() ''This must be done by your calling function now 
    ''btw, if you forget, it may cause connection leaks, which are evil 
    Return conn 
End function 
End class 

例2:

Public Class Class1 
Public conn As MySqlConnection 
Dim command As MySqlCommand 
Dim reader As MySqlDataReader 

''open the connection in the constructor 
Sub New() 
    conn = New MySqlConnection 
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword') 
    Try 
     conn.Open() 

     ''MessageBox.Show("Connected!") 
    Catch x As Exception 
     MsgBox(x.Message) 
    End Try 
End Sub 

''close the connection in the destructor 
Protected Overrides Sub Finalize() 
    conn.Close() ''automatically runs when this class is garbage collected 
    conn.Dispose() 
End Sub 
End class 
関連する問題