2012-07-24 42 views
-1

我是新的vb.net,sql甚至記錄在phpmyadmin中的數據。當我使用我的界面添加記錄時,我檢查了我的phpmyadmin,它指示表中有一個條目,表示我的vb.net和phpmyadmin已連接,但值返回NULL。我怎樣才能讓它不是NULL?相反,記錄所需的數據?vb.net保存在phpmyadmin空值返回

這是我的代碼:

Dim ServerString As String = "Server=localhost; database=patientinfo;user id=root;password=" 
Dim SQLConnection As New MySqlConnection() 

Public Sub SaveInfos(ByRef SQLStatement As String) 
    Dim cmd As MySqlCommand = New MySqlCommand 

    With cmd 
     .CommandText = SQLStatement 
     .CommandType = CommandType.Text 
     .Connection = SQLConnection 
     .ExecuteNonQuery() 

    End With 
    SQLConnection.Close() 

    'MsgBox("Successfully Added!") 
    MsgBox("Successfully Saved") 
    Me.Visible = False 
    Mainform.Show() 

    SQLConnection.Dispose() 
End Sub 

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click 
    Try 
     '===========================Add New Patient======================================== 
     bednumber = txtbednum.Text 
     patient_name = txtName.Text 
     patient_age = Convert.ToInt32(txtAge.Text) 
     date_of_confinement = Date.Parse(dtpDate.Value) 
     type_of_sickness = txtSickness.Text 
     type_of_IV_fluid = txtFluid.Text 
     number_of_bottles = Convert.ToInt32(txtBottle.Text) 
     drop_rate = Convert.ToInt32(txtDrop.Text) 

     'To check if all values have been filled up 
     If patient_name = "" Or patient_age = "" Or date_of_confinement = "" _ 
     Or type_of_sickness = "" Or type_of_IV_fluid = "" Or number_of_bottles = "" Or drop_rate = "" _ 
     Then 

      MsgBox("Please Complete All the Required Fields") 

     Else 

      Dim SQLStatement As String = "INSERT INTO patient(bednumber, name, age, date_of_confinement,type_of_sickness, type_of_IV_fluid, number_of_bottles, drop_rate) VALUES(@bednumber, @name, @age, @date_of_confinement, @type_of_sickness, @type_of_IV_fluid, @number_of_bottles, @drop_rate)" 
      SaveInfos(SQLStatement) 

這就是我的表看起來像在phpMyAdmin:

http://i1182.photobucket.com/albums/x454/catherinefbalauro/Untitled.png

任何幫助,將不勝感激。謝謝! :)

+0

已經解決了由我:))[CLOSED] – Mineko 2012-07-26 01:21:43

回答

0

做這樣的事情:

' Assuming that your SQLStatement is as follow 
' INSERT INTO MyTableName 
' VALUES ( @bednumber, @name, @age, 
'   @date_of_confinement,@type_of_sickness, 
'   @type_of_IV_fluid, @number_Of_Bottles, 
'   @drop_rate 
'  ) 
With cmd 
    .CommandText = SQLStatement 
    .CommandType = CommandType.Text 
    .Connection = SQLConnection 

    .Parameters.AddWithValue("@bednumber", txtbednum.Text) 
    .Parameters.AddWithValue("@name", txtName.Text) 
    .Parameters.AddWithValue("@age", txtAge.Text) 
    ' dothe same until 
    ' you have added allthe parameter 
    ' with values 

    SQLConnection.Open() 
    .ExecuteNonQuery() 
End With 
+0

vb.net不能接受.addwithvalue,因爲它說,它不是MySqlParameterCollection OO成員 – Mineko 2012-07-24 15:51:23

+0

沒有,是應該有工作..看看這段代碼http://stackoverflow.com/a/10524452/491243 – 2012-07-24 16:26:41

+0

C#的語法和vb是不是一樣吧?當我嘗試你的建議時說它不是mysqlparameter的成員。 我的代碼接受的是這樣的: .Parameters.Add(新的MySqlParameter(「@ bednumber」,txtbednum.Text)) 但仍NULL – Mineko 2012-07-24 16:42:39