2012-11-21 145 views
-1

嘿傢伙我需要一些幫助我的代碼在這裏我真的不知道它在哪裏關閉連接的部分....我想要的是保持添加記錄,直到我點擊了msgbox沒有什麼關閉此連接?

繼承人我的代碼:

Dim connstr As String = "server=localhost; uid= root; password=root; database=lmsdbase;" 
Dim conn As MySqlConnection = New MySqlConnection 
Dim ds As New DataSet 
Dim da As MySqlDataAdapter 


    conn.ConnectionString = connstr 



    Try 
     If conn.State = ConnectionState.Closed Then 
      conn.Open() 
     Else 
      conn.Close() 
      MsgBox("Database Connection Error") 
     End If 
    Catch ex As Exception 
     MsgBox(Err.Description) 

     txtBookISBN.Focus() 

    End Try 



    Dim SqlStatement As String = "INSERT INTO tblIssue(ISBN, Title, BorrowerID, Name, Occupation, BorrowerType, School, DateIssued, DateToBeReturned) VALUES('" & txtBookISBN.Text & "','" & txtBTitle.Text & "','" & txtMemID.Text & "','" & txtMemName.Text & "','" & txtOccupation.Text & "','" & cboBtype.Text & "','" & txtSchool.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "')" 
    Save(SqlStatement) 

    Public Sub Save(ByRef SqlStatement As String) 
    Dim cmd As MySqlCommand = New MySqlCommand 

    With cmd 

     .CommandText = SqlStatement 
     .CommandType = CommandType.Text 
     .Connection = conn 
     .ExecuteNonQuery() 

    End With 

    'conn.Close() 
    MsgBox("Success!") 


    If MsgBox("Do You Want to Add New Record?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then 



     txtMemID.Text = "" 
     txtMemName.Text = "" 
     txtBookISBN.Text = "" 
     txtBTitle.Text = "" 
     txtOccupation.Text = "" 
     cboBtype.Text = "" 
     txtSchool.Text = "" 

     txtMemID.Focus() 

    Else 


     Me.Close() 


    End If 

    conn.Dispose() 

End Sub 

真的很感謝你的幫助

+2

執行中是否有錯誤? – Gayatri

回答

1
If conn.State = ConnectionState.Closed Then 
      conn.Open() 
     Else 
      conn.Close() 
      MsgBox("Database Connection Error") 

難道這不是關閉所有打開的連接?

+0

你爲什麼不調試和檢查? –

+2

已經知道了...它與我放入的其他代碼有衝突。 –

+0

@BoyGeorge George:幹得好:) –