如果记录存在更新,则插入sql vb.net

时间:2014-09-29 13:31:55

标签: sql sql-server vb.net visual-studio-2013

我有以下问题,我正在使用vb.net开发诊所应用程序,医生可以使用复选框添加医疗信息checkbox2.text =" Allergy" textbox15.text是过敏的注释,如果患者的FileNo(Textbox2.text)不存在,我想插入记录,如果确实只是更新了注释,到目前为止我能够点击3次按钮后更新它我不知道为什么????

任何帮助表示赞赏:) 提前谢谢

    Dim connection3 As New SqlClient.SqlConnection
    Dim command3 As New SqlClient.SqlCommand
    Dim adaptor3 As New SqlClient.SqlDataAdapter
    Dim dataset3 As New DataSet
    connection3.ConnectionString = ("Data Source=(LocalDB)\v11.0;AttachDbFilename=" + My.Settings.strTextbox + ";Integrated Security=True;Connect Timeout=30")
    command3.CommandText = "SELECT ID,Type FROM Medical WHERE FileNo='" & TextBox2.Text & "';"
    connection3.Open()
    command3.Connection = connection3
    adaptor3.SelectCommand = command3
    adaptor3.Fill(dataset3, "0")
    Dim count9 As Integer = dataset3.Tables(0).Rows.Count - 1
    If count9 > 0 Then
        For countz = 0 To count9
            Dim A2 As String = dataset3.Tables("0").Rows(countz).Item("Type").ToString
            Dim B2 As Integer = dataset3.Tables("0").Rows(countz).Item("ID")
            TextBox3.Text = A2
            If A2 = CheckBox1.Text Then
                Dim sql4 As String = "update Medical set MNotes=N'" & TextBox22.Text & "' where FileNo='" & TextBox2.Text & "' and Type = '" & CheckBox1.Text & "' and ID='" & B2 & "';"
                Dim cmd4 As New SqlCommand(sql4, connection3)
                Try
                    cmd4.ExecuteNonQuery()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            ElseIf A2 = CheckBox2.Text Then
                Dim sql4 As String = "update Medical set MNotes=N'" & TextBox15.Text & "' where FileNo='" & TextBox2.Text & "' and Type = '" & CheckBox2.Text & "' and ID='" & B2 & "';"
                Dim cmd4 As New SqlCommand(sql4, connection3)
                Try
                    cmd4.ExecuteNonQuery()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Next
    Else
        If CheckBox1.Checked = True Then
            Dim sql4 As String = "insert into Medical values('" & CheckBox1.Text & "',N'" & TextBox22.Text & "','" & TextBox2.Text & "')"
            Dim cmd4 As New SqlCommand(sql4, connection3)
            Try
                cmd4.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        If CheckBox2.Checked = True Then
            Dim sql4 As String = "insert into Medical values('" & CheckBox2.Text & "',N'" & TextBox15.Text & "','" & TextBox2.Text & "')"
            Dim cmd4 As New SqlCommand(sql4, connection3)
            Try
                cmd4.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End If

2 个答案:

答案 0 :(得分:1)

我认为你的一个问题可能与你将表行数减少1然后在0以上测试有关:

Dim count9 As Integer = dataset3.Tables(0).Rows.Count - 1
If count9 > 0 Then

尝试更改为:

Dim count9 As Integer = dataset3.Tables(0).Rows.Count
If count9 > 0 Then

另外,请确保勾选了代码中稍后提到的其中一个复选框(CheckBox1CheckBox2)。

- 编辑 -

对不起 - 没解释原因!原因是.NET中的大多数数组/列表结构都是基于零的(即从0开始计数而不是1)。

答案 1 :(得分:0)

最适合您的行动是通过允许SQL尽其所能来最大限度地提高您的工作效率。假设您使用的是SQL Server 2008,MERGE函数是您提供的条件的绝佳用例。

这是一个基于您的一些代码设计的非常基本的示例:

CREATE PROCEDURE [dbo].[csp_Medical_Merge]
    @MType int, @FileNo varchar(20), @MNotes varchar(max), @ID int,  @OtherParams
AS
BEGIN

MERGE INTO [DatabaseName].dbo.Medical AS DEST
USING 
    ( 
        /*
            Only deal with data that has changed.  If nothing has changed,
            then move on.
        */
        SELECt @MType, @MNotes, @FileNo, @ID, @OtherParams
        EXCEPT
        Select [Type], MNotes, FileNo, ID, OtherFields from [DatabaseName].dbo.Medical
) As SRC ON SRC.ID = DEST.ID
WHEN MATCHED THEN
    UPDATE SET
        DEST.MNOTES = SRC.MNOTES,
        DEST.FileNo = SRC.FileNo,
        DEST.Type = SRC.Type
WHEN NOT MATCHED BY TARGET THEN
    INSERT (FieldsList) 
    VALUEs (FileNo, MNotes, etc, etc);

END
GO