none
vb2008 使用SqlBulkCopy指令存入資料庫前,能否驗證是否有相同的資料存在資料庫中? RRS feed

  • 問題

  • 親愛的各位大大:

    我使用SqlBulkCopy指令將Datagridview的資料存入Sql伺服器的某Table中。請問有沒有辦法可以判斷即將存入的資料是否存在資料庫中?

    以下是我的程式碼,感謝各位大大。

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyBulkcopy As SqlBulkCopy
            Dim TA003, TA001, TA002, TB003, ME002, MV047, TA013, TB004, TB005, TB006, TB009,

                   TB007, TB011, TB032, TB043, TA016, TB039, MA002, ProDate, ProClass, Buyer As

                   SqlBulkCopyColumnMapping
            Dim myDatatable3 As New DataTable
            If myDatatable1.Rows.Count = 0 Then
                MessageBox.Show("無資料無法新增")
            Else
                myDatatable3 = DataGridView1.DataSource
                MyBulkcopy = New SqlBulkCopy(strDbCon)
                MyBulkcopy.DestinationTableName = "SampleQuery"
                TA003 = New SqlBulkCopyColumnMapping("TA003", "TA003")
                TA001 = New SqlBulkCopyColumnMapping("TA001", "TA001")
                TA002 = New SqlBulkCopyColumnMapping("TA002", "TA002")
                TB003 = New SqlBulkCopyColumnMapping("TB003", "TB003")
                ME002 = New SqlBulkCopyColumnMapping("ME002", "ME002")
                MV047 = New SqlBulkCopyColumnMapping("MV047", "MV047")
                TA013 = New SqlBulkCopyColumnMapping("TA013", "TA013")
                TB004 = New SqlBulkCopyColumnMapping("TB004", "TB004")
                TB005 = New SqlBulkCopyColumnMapping("TB005", "TB005")
                TB006 = New SqlBulkCopyColumnMapping("TB006", "TB006")
                TB009 = New SqlBulkCopyColumnMapping("TB009", "TB009")
                TB007 = New SqlBulkCopyColumnMapping("TB007", "TB007")
                TB011 = New SqlBulkCopyColumnMapping("TB011", "TB011")
                TB032 = New SqlBulkCopyColumnMapping("TB032", "TB032")
                TB043 = New SqlBulkCopyColumnMapping("TB043", "TB043")
                TA016 = New SqlBulkCopyColumnMapping("TA016", "TA016")
                TB039 = New SqlBulkCopyColumnMapping("TB039", "TB039")
                MA002 = New SqlBulkCopyColumnMapping("供應商", "MA002")
                ProDate = New SqlBulkCopyColumnMapping("ProDate", "ProDate")
                ProClass = New SqlBulkCopyColumnMapping("樣品類別", "ProClass")
                Buyer = New SqlBulkCopyColumnMapping("採購人員", "Buyer")
                MyBulkcopy.ColumnMappings.Add(TA003)
                MyBulkcopy.ColumnMappings.Add(TA001)
                MyBulkcopy.ColumnMappings.Add(TA002)
                MyBulkcopy.ColumnMappings.Add(TB003)
                MyBulkcopy.ColumnMappings.Add(ME002)
                MyBulkcopy.ColumnMappings.Add(MV047)
                MyBulkcopy.ColumnMappings.Add(TA013)
                MyBulkcopy.ColumnMappings.Add(TB004)
                MyBulkcopy.ColumnMappings.Add(TB005)
                MyBulkcopy.ColumnMappings.Add(TB006)
                MyBulkcopy.ColumnMappings.Add(TB009)
                MyBulkcopy.ColumnMappings.Add(TB007)
                MyBulkcopy.ColumnMappings.Add(TB011)
                MyBulkcopy.ColumnMappings.Add(TB032)
                MyBulkcopy.ColumnMappings.Add(TB043)
                MyBulkcopy.ColumnMappings.Add(TA016)
                MyBulkcopy.ColumnMappings.Add(TB039)
                MyBulkcopy.ColumnMappings.Add(MA002)
                MyBulkcopy.ColumnMappings.Add(ProDate)
                MyBulkcopy.ColumnMappings.Add(ProClass)
                MyBulkcopy.ColumnMappings.Add(Buyer)
                MyBulkcopy.WriteToServer(myDatatable3)
                MessageBox.Show("已存檔")
            End If
        End Sub

    請問如果要判斷是否存在資料庫中程式碼該如何寫呢?感謝各位大大。

    2010年7月21日 上午 01:56

解答