2017-12-17 6 views
-1

私は個人情報を追加するプログラムを新入社員に追加しています。そこにはID(例:税番号)があります。しかし、私は新しいテーブルを作成して給与のための新しいテーブルを作成し、そこにある列の利点id(ex.tax番号)が、私は新しい従業員がここに追加された後に自動的にテーブルを作成したい私のコードですそれはあなたを助ける場合:vb.netテーブルを作成します。テーブルの列ごとにtextbox.textを使用

をところでIMは「 '@idd' 付近に正しくない構文を示しMESSAGEBOXを取得

 Dim add As String = String.Empty 
    add &= "insert into rec_member(firstname,middlename,lastname,age,birthday,pagibig,philhealth,sss,tin,department)" 
    add &= "values " 
    add &= "(@first,@middle,@last,@age,@bday,@pagibig,@philhealth,@sss,@tin,@dept);select scope_identity()" 

    Dim benefits As String = String.Empty 
    benefits &= "create table @idd(" & _ 
    "pagibig(@ibig) integer not null, " & _ 
    "philhealth(@phil) integer not null," & _ 
    "sss(@rsss) integer not null," & _ 
    "tin(@rtin) integer not null)" 

    Using conn As New SqlConnection("server=WIN10;database=add_member;user=hradmin;password=admin;") 
     Using cmd As New SqlCommand 
      With cmd 
        .Connection = conn 
       .CommandType = CommandType.Text 
       .CommandText = add 
       .Parameters.Add("@first", SqlDbType.VarChar).Value = afirstname.Text 
       .Parameters.Add("@middle", SqlDbType.VarChar).Value = amiddlename.Text 
       .Parameters.Add("@last", SqlDbType.VarChar).Value = alastname.Text 
       .Parameters.Add("@age", SqlDbType.Int).Value = aage.Value 
       .Parameters.Add("@bday", SqlDbType.Date).Value = abirthday.Text 
       .Parameters.Add("@pagibig", SqlDbType.Int).Value = apagibig.Text 
       .Parameters.Add("@philhealth", SqlDbType.Int).Value = aphilhealth.Text 
       .Parameters.Add("@sss", SqlDbType.Int).Value = asss.Text 
       .Parameters.Add("@tin", SqlDbType.Int).Value = atin.Text 
       .Parameters.Add("@dept", SqlDbType.VarChar).Value = adepartment.SelectedItem 
      End With 


       Try 
        conn.Open() 
        If afirstname.Text.Length < 2 Then 
         MsgBox("Please input more value on the FIRST NAME") 
        ElseIf amiddlename.Text.Length < 2 Then 
         MsgBox("Please input more value on the MIDDLE NAME") 
        ElseIf alastname.Text.Length < 2 Then 
         MsgBox("Please input more value on the LAST NAME") 
        ElseIf aage.Value < 16 Then 
         MsgBox("Age must be appropriate") 
        ElseIf abirthday.Text > "1997-01-01" Then 
         MsgBox("Please select a birthday") 
        ElseIf apagibig.Text.Length < 5 Then 
         MsgBox("Please input more value on the PAG-IBIG") 
        ElseIf adepartment.SelectedItem = "" Then 
         MsgBox("Please Select a Department") 
        ElseIf aphilhealth.Text.Length < 5 Then 
         MsgBox("Please input more value on the PHILHEALTH") 
        ElseIf asss.Text.Length < 5 Then 
         MsgBox("Please input more value on th SSS") 
        ElseIf atin.Text.Length < 5 Then 
         MsgBox("Please input more value on the TIN") 
        Else 

         Dim id As Integer = CInt(cmd.ExecuteScalar) 
         MsgBox("NEW EMPLOYEE ADDED" & Environment.NewLine & 
           "ID NUMBER:" & id & Environment.NewLine & 
           "FIRST NAME:" & afirstname.Text & Environment.NewLine & 
           "MIDDLE NAME:" & amiddlename.Text & Environment.NewLine & 
           "LAST NAME:" & alastname.Text & Environment.NewLine & 
           "AGE:" & aage.Value & Environment.NewLine & 
           "BIRTHDAY:" & abirthday.Text & Environment.NewLine & 
           "PAG-IBIG:" & apagibig.Text & Environment.NewLine & 
           "PHIL-HEALTH:" & aphilhealth.Text & Environment.NewLine & 
           "SSS:" & asss.Text & Environment.NewLine & 
           "TIN:" & atin.Text & Environment.NewLine & 
           "DEPARTMENT:" & adepartment.SelectedItem & Environment.NewLine 
           ) 

        Using cmd1 As New SqlCommand 
         With cmd1 
           .Connection = conn 
          .CommandType = CommandType.Text 
          .CommandText = benefits 
          .Parameters.Add("@idd", SqlDbType.Int).Value = id 
          .Parameters.Add("@ibig", SqlDbType.Int).Value = apagibig.Text 
          .Parameters.Add("@phil", SqlDbType.Int).Value = aphilhealth.Text 
          .Parameters.Add("@rsss", SqlDbType.Int).Value = asss.Text 
          .Parameters.Add("@rtin", SqlDbType.Int).Value = atin.Text 
         End With 

         cmd1.ExecuteNonQuery() 
         MsgBox("New Table is Set for new employee") 
        End Using 
       End If 
       afirstname.Clear() 
       amiddlename.Clear() 
       alastname.Clear() 
       aage.Value = 0 
       abirthday.Text = "1997-01-01" 
       apagibig.Clear() 
       adepartment.ResetText() 
       aphilhealth.Clear() 
       asss.Clear() 
       atin.Clear() 
       conn.Close() 

      Catch ex As Exception 
       MsgBox(ex.Message) 
      End Try 


      End Using 
     End Using 
+0

あなたは間違って自分のパラメータを追加しています。関連するドキュメントを読んで、 'Add'と' AddWithValue'の違いを学んでください。 – jmcilhinney

+0

実際、最初のパラメータには 'Add'を使用していますが、残りのすべてについては構文を変更せずに' AddWithValue'を使用しています。もう一度、それぞれの仕組みを学びます。 – jmcilhinney

+0

ああ、私はそれを編集することをお待ち申し上げます。 – Camorra

答えて

1

問題がある:。

create table @idd ... 

あなたが非でコマンドを送信カント静的テーブル名

このため

は、あなたがトークンと、コマンドテキストを構築する必要があります。

benefits &= "create table {0}(" & _ 
"pagibig(@ibig) integer not null, " & _ 
"philhealth(@phil) integer not null," & _ 
"sss(@rsss) integer not null," & _ 
"tin(@rtin) integer not null)" 

と:

Using cmd1 As New SqlCommand 
    With cmd1 
     .Connection = conn 
     .CommandType = CommandType.Text 
     .CommandText = String.Format(benefits, id) 
     .Parameters.Add("@ibig", SqlDbType.Int).Value = apagibig.Text 
     .Parameters.Add("@phil", SqlDbType.Int).Value = aphilhealth.Text 
     .Parameters.Add("@rsss", SqlDbType.Int).Value = asss.Text 
     .Parameters.Add("@rtin", SqlDbType.Int).Value = atin.Text 
+0

私のID NUMBERをテーブル名にする方法はありますか? – Camorra

+0

@Camorraはい...私のコード例はこれを行います。 '{0}'トークンは 'id'変数に置き換えられました。 – dovid

+0

このコードは何ですか? '.CommandText = String.Format(benefits、id)'です。私はそれをかなり理解していない。 – Camorra

関連する問題