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

如果记录存在,请更新或插入sql vb.net,sql,sql-server,vb.net,visual-studio-2013,Sql,Sql Server,Vb.net,Visual Studio 2013,我有以下问题,我正在使用vb.net开发一个临床应用程序,医生可以使用复选框checkbox2.text=Allergy textbox15.text添加医疗信息。text是过敏注释,如果患者的文件notextbox2.text不存在,我想插入记录,如果存在,则只更新注释,到目前为止,我能够更新后,3个按钮点击我不知道为什么 感谢您的帮助: 提前谢谢 Dim connection3 As New SqlClient.SqlConnection Dim command3 As Ne

我有以下问题,我正在使用vb.net开发一个临床应用程序,医生可以使用复选框checkbox2.text=Allergy textbox15.text添加医疗信息。text是过敏注释,如果患者的文件notextbox2.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

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

Dim count9为整数=dataset3.table0.Rows.Count-1 如果count9>0,则 尝试更改为:

Dim count9为整数=dataset3.table0.Rows.Count 如果count9>0,则 另外,确保代码后面提到的复选框CheckBox1或CheckBox2中的一个被勾选

-编辑-


对不起,没有解释原因!原因是.NET中大多数类似数组/列表的结构都是以零为基础的,即从0开始计算,而不是从1开始计算。

对您来说,最好的做法是通过允许SQL做它最擅长的事情来最大限度地提高生产效率。假设您使用的是SQL Server 2008,该函数对于您提供的条件来说是一个很好的用例

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

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

您使用什么数据库?sql server数据库文件您应该使用参数来执行类似操作。command3.CommandText=选择ID,从Medical中键入,其中FileNo='&TextBox2.Text&';是一种可靠的接收方法。此代码极易受到sql注入攻击。实际上,被黑客入侵是一件很痛苦的事情。不过,MAX,这是一个很好的实践,甚至可能为你的项目赢得额外的分数。索引超出范围异常在位置1没有行程序停在哪一行?还有,TextBox2中有什么?在Dim A2上,字符串和TextBox2包含患者的文件号Rok。调试时,可以通过将指针放在变量名上来测试变量值。测试countz的值,看看它在哪里中断。。。