2012-07-18 2 views
0

저는 Visual Studio 및 SQL Server의 새로운 기능입니다.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 서버 관리 설계 내 데이터베이스의 열 중 하나입니다 내 데이터베이스에 텍스트 상자에 입력 된 값을 추가합니다. 어떤 도움을 주시면 감사하겠습니다. 감사!

내가 insert.Parameters.AddWithValue("@name", txtName.Text) 변경있어하고 SQL = "이 제안 내용과 모델 부분에 삽입하지만 오류

을 얻었다는"형식의 첫째 예외가 'System.InvalidCastException' 은 Microsoft.VisualBasic 발생했습니다. dll을 "

내가 혼란

답변

2

당신이 가까이 있습니다.

하나의 변수 이름을 사용해보십시오.

그래서 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) 
+0

내가 Microsoft.VisualBasic에서 발생한 'System.InvalidCastException'형식의 첫째 예외 "의 오류가 발생했습니다. dll "나는 혼란 스럽다. – Mineko

+0

유효하지 않은 유형 또는 데이터베이스 값과 일치하지 않는 유형을 전달하려고 시도하는 것 같습니다. "toString()"을 사용하여 값을 설정하려고하면 매개 변수에 DbType을 지정한 다음 값 유형이 데이터베이스 테이블과 동일한지 확인하는 것이 가장 좋습니다 (int, int, varchar/char 문자열로) – RiddlerDev

+0

샘플 구문을 제공 할 수 있습니까? 감사! – Mineko

관련 문제