2014-09-29 86 views
1

我有以下問題,我正在開發一個使用vb.net的診所應用程序,醫生有能力使用複選框添加醫療信息checkbox2.text =「Allergy」textbox15。文本是過敏的注意事項,如果病人的FileNo(Textbox2.text)不存在,我想插入記錄,如果它只是更新註釋,到目前爲止我能夠在點擊3次按鈕後更新它不知道爲什麼?如果記錄存在更新else insert sql vb.net

任何幫助表示讚賞:) 預先感謝

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 
+0

你用什麼數據庫? – zaratustra 2014-09-29 13:34:52

+0

sql服務器數據庫文件 – MAX 2014-09-29 13:36:11

+1

您應該使用參數來執行此類操作。 'command3.CommandText =「選擇ID,鍵入從醫療WHERE FileNo ='」和TextBox2.Text & "';「'是一種確定的接收[SQL注入攻擊]的火法(http://en.wikipedia.org/wiki/SQL_injection ) – Paul 2014-09-29 13:38:37

回答

1

我覺得你的問題之一可能與您減少1表格的行數,然後測試它上面0:

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

嘗試改用:

此外,確保一個代碼中後面提到的複選框(CheckBox1CheckBox2)被勾選。

- 編輯 -

抱歉 - 沒有解釋爲什麼!原因是.NET中的大部分數組/列表結構都是基於零的(即從0開始而不是從1開始計數)。

+0

索引超出範圍異常「在位置1沒有行「 – MAX 2014-09-29 13:54:31

+0

該程序停止在哪條線上? – Paul 2014-09-29 13:55:39

+0

另外,」TextBox2「中有什麼? – Paul 2014-09-29 13:56:28

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 
相關問題