0
我是Visual Studio和SQL Server的新成員。我糊塗瞭如何從Visual Studio應用程序如何使用SqlCommand將數據保存到sql數據庫
插入數據這是我的代碼:
嘗試
'===============Add New Patient===================
patient_name = txtName.Text
patient_age = nudAge.Value
date_of_confinement = dtpDate.Value
type_of_sickness = txtSickness.Text
type_of_IVfluid = txtFluid.Text
number_of_bottles = txtBottle.Text
drop_rate = nudDrop.Value
'To check if all values have been filled up
If txtName.Text = "" Or nudAge.Value = "" Or dtpDate.Value = "" _
Or txtSickness.Text = "" Or txtFluid.Text = "" Or txtBottle.Text = "" Or nudDrop.Value = "" _
Then
MsgBox("Please Complete All the Required Fields")
Else
Try
Dim PatientInfoConnection As SqlConnection = New _
SqlConnection("Server=CATH-PC; database=PatientInfoDB;user id=sa;password=*******") 'connection to SQL database
PatientInfoConnection.Open() 'open database connection
Dim sql As String = ""
sql = "insert into model (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
" values (@txtName.Text, @nudAge.Value, @dtpDate.Value, @txtSickness.Text, @txtFluid.Text, @txtBottle.Text, @nudDrop.Value)"
Dim insert As New SqlCommand(sql, PatientInfoConnection)
insert.Parameters.AddWithValue(patient_name, @txtName.Text)'@ error
insert.Parameters.AddWithValue("val2", nudAge.Value) 'error
insert.Parameters.AddWithValue(Name, patient_name) 'not sure
insert.ExecuteNonQuery()
MsgBox("Successfully Saved")
Me.Visible = False
Mainform.Show()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message & ". Please contact the operator")
End Try
End If
Catch ex As Exception
End Try
我不知道有關SqlCommand
,因爲我不知道如何保存用戶輸入到我的數據庫。我想在一個文本框中輸入值,添加到我的數據庫
Dim insert As New SqlCommand(sql, PatientInfoConnection)
insert.Parameters.AddWithValue(patient_name, @txtName.Text) 'error @ char is not valid
insert.Parameters.AddWithValue("val2", nudAge.Value) 'error
insert.Parameters.AddWithValue(Name, patient_name) 'not error but not sure
name(text,null)
是我的數據庫,我在SQL Server Management Studio的設計
請幫我的一列。任何幫助,將不勝感激。謝謝!
我得到了改變insert.Parameters.AddWithValue("@name", txtName.Text)
和SQL =「插入模型一部分什麼建議,但我得到一個錯誤
」類型的第一次機會異常則‘’ 發生在Microsoft.VisualBasic程序。 DLL」
我很困惑
我的「 '' 類型發生在Microsoft.VisualBasic程序的第一個機會異常錯誤。 DLL「我很困惑。 – Mineko 2012-07-18 02:23:54
可能您正在嘗試傳遞無效類型或與數據庫值不一致的類型。你可以嘗試用「toString()」來設置你的值,最好的做法是爲參數指定DbType,然後驗證值類型是否與數據庫表相同(int爲int,varchar/char以字符串) – RiddlerDev 2012-07-18 02:27:14
你可以給出示例語法?謝謝! – Mineko 2012-07-18 02:31:23