vb.net创建表。使用textbox.text作为表

时间:2017-12-17 07:39:12

标签: sql-server vb.net

我有一个程序,添加新员工,它添加了个人信息和利益id(例如税号)。但我想做的是通过制作一个新表创建一个新的工资表,列将是有利益id(ex.tax数字)但我想在新员工添加到这里后自动创建表是我的代码如果它会帮助你:

BTW IM获取一个消息框,显示" @ idd'附近的语法不正确。

     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

1 个答案:

答案 0 :(得分: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