【问题标题】:Insert Data into Foreign Key Table VB.NET将数据插入外键表 VB.NET
【发布时间】:2017-09-18 11:19:39
【问题描述】:

如何将数据插入外键表?

我在数据库AddemployeesNormal_L 中有两个表包含一个关系,即ID。我想将数据插入Normal_L 表中,其中ID 必须与Addemployees 表保持相关。

Dim cmd As New System.Data.SqlClient.SqlCommand
    Dim RA As Integer
    Try
        cn.Open()
        cmd.CommandType = System.Data.CommandType.Text
        If Me.RadioButton1.Checked Then
            cmd = New SqlCommand("INSERT into Normal_L (fromD,toD,DScrip) VALUES ('" & DateTimePicker2.Text & "','" & DateTimePicker1.Text & "','" & TextBox5.Text & "') where ID ='" & ComboBox1.Text & "' ", cn)
            cmd.Connection = cn
            RA = cmd.ExecuteNonQuery()
            MessageBox.Show("Process successful!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cn.Close()
        End If
    Catch
        MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()

End Sub

我希望你能帮我解决这个问题

【问题讨论】:

  • 插入 ... 不存在的地方。使用外键,如果 Addemployees 中不存在外键,则您不应该能够插入 Normal_L。请注意,如果 Normal_L.ID 上的外键约束不匹配,则会引发错误
  • 你介意告诉我如何完成这项工作

标签: sql-server vb.net


【解决方案1】:

好吧,如果你想正确使用存储过程:

CREATE PROCEDURE [dbo].[YourProcName]
    -- Add the parameters for the stored procedure here
     @fromD nvarchar(50), --yourType
     @toD nvarchar(50), --Type
     @DScrip nvarchar(50), --yourType
     @ID int
 AS
 BEGIN
     -- SET NOCOUNT ON added to prevent extra result sets from
     -- interfering with SELECT statements.
    SET NOCOUNT ON;

    INSERT into Normal_L (fromD,toD,DScrip) 
    SELECT  @fromD ,
            @toD,
            @DScrip
    FROM Addemployees 
    WHERE Addemployees.ID = @ID
END

这个存储过程不仅可以防止您之前代码中的 sql 注入,而且如果您仔细阅读,则只有在 Addemployee 中的 id 与您的参数匹配时,选择才会选择某些内容。同样,您可以使用简单的外键,但由于我不知道您的基础结构,所以我无法分辨。

现在为您的 VB 代码:

Dim cmd As New System.Data.SqlClient.SqlCommand
    Dim RA As Integer
    Try
        cn.Open()
        cmd.CommandType = System.Data.CommandType.StoredProcedure
        If Me.RadioButton1.Checked Then
            cmd = New SqlCommand("YourProcName", cn)
            cmd.Connection = cn
            cmd.Parameters.AddWithValue("@fromD", DateTimePicker2.Text)
            cmd.Parameters.AddWithValue("@toD",DateTimePicker1.Text )
            cmd.Parameters.AddWithValue("@DScrip",TextBox5.Text )
            cmd.Parameters.AddWithValue("@ID", ComboBox1.Text)
            RA = cmd.ExecuteNonQuery()
            MessageBox.Show("Process successful!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cn.Close()
        End If
     Catch
        MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()

 End Sub

您需要更改存储过程中的类型,我不知道您在使用什么。您的最终代码应大致如下所示。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多