0

VisualStudioとSQLServerは初めてです。VisualStudioアプリケーションからデータを挿入する方法に混乱しました

これは私のコードです:

試す

'===============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 ServerManagementStudioで設計したデータベースの列の1つです

私を助けてください。どんな助けでもいただければ幸いです。ありがとう!

変更されinsert.Parameters.AddWithValue("@name", txtName.Text)、sql = "提案どおりにモデル部分に挿入されましたが、エラーが発生しました

「タイプ'System.InvalidCastException'の最初のチャンスの例外がMicrosoft.VisualBasic.dllで発生しました」

私は混乱しています

4

1 に答える 1

2

あなたは近くにいます。

変数名を1つだけ試して、それらを一致させてください。

したがって、SQLは次のようになります。

sql = "insert into model (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                   " values (@name, @age, @dateOfConfinement, @sicknessType, @fluidType, @bottleAmount, @dropRate)"

次に、次のようなパラメータを設定します。

insert.Parameters.AddWithValue("@name", txtName.Text)
于 2012-07-18T02:14:03.103 に答える