帮助两个数据表之间的比较语法 [英] Help With Comparison Syntax Between Two Datatables

查看:78
本文介绍了帮助两个数据表之间的比较语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个数据表,并根据客户编号生成不匹配的行的结果数据表.主表客户"包含已放置在我们系统中的所有客户.我正在尝试生成一个客户表"dtResults",我们从未从名为"CurrentCust"的表生成发票.

我希望有人查看所附的代码,看看如何处理"InvalidCastException未处理",无法转换"类型为'System.String'的对象,以键入'CustomerDataTable'错误.错误发生在第43行.

I am trying to compare two datatables and generate a resultant datatable of rows that do not match based on customer number.  The main table "Customers" contains all customers that have been placed in our system.  I am trying to generate a table of customers, "dtResults," that we have not ever generated an invoice from a table called "CurrentCust".

I would like someone to look at the attached code and see how I can handle the 'InvalidCastException was unhandled', 'Unable to cast object of type 'System.String' to type 'CustomerDataTable' error.  Error occurs at line 43.

<身体>
1 选项 显式 打开
2 选项 严格关闭
3
4 导入 excel = Microsoft.Office. Interop.Excel.Application
5 导入 wBook = Microsoft.Office.Interop.Excel.Workbook
6 导入 wSheet = Microsoft.Office. Interop.Excel.Sheets
7
8 公共 frmOpenCustomers
9
10 私有 frmOpenCustomers_Load( ByVal 发件人 As 系统. 对象 ByVal 句柄 MyBase "TODO":这行代码将数据加载到"dbSA_CUSTOMERLISTDataSet.Customer"表中.您可以根据需要移动或删除它.
12 .CustomerTableAdapter.Fill( .dbSA_CUSTOMERLISTDataSet.Customer)
13
14 结束
15
16 私有 btnRun_Click( ByVal 发件人 As 系统. 对象 ByVal 句柄 设置数据表以保存所有客户的比较结果到已在系统中生成发票的客户数据表中.
18 昏暗 dtResults DataRow
20 为dtResults定义数据列.
21 dtResults.Columns.Add( " CSCODE" GetType ( String dtResults.Columns.Add( " SANAME" GetType String ))
23 dtResults.Columns.Add( " CSNAME" GetType ( String dtResults.Columns.Add( " CSADDR1" GetType String ))
25 dtResults.Columns.Add( " CSADDR2" GetType ( String dtResults.Columns.Add( " CSCITY" GetType String ))
27 dtResults.Columns.Add( " CSST" GetType ( String dtResults.Columns.Add( " CSZIP" GetType String ))
29 dtResults.Columns.Add( " CSTYPE" GetType ( String dtResults.Columns.Add( " CSENTDATE" GetType String ))
31 dtResults.Columns.Add( " CSMODDATE" GetType ( String dtResults.Columns.Add( " CSPHONE" GetType String ))
33 '定义查询参数.
34 wasfound 布尔值 错误 a 整数 = 0
36 昏暗 b 整数 MaxRowCurrentCust 整数 == 输入 ( .CustomerTableAdapter.ScalarQueryCurrentCustNumber.Value)-1
38 昏暗 MaxRowCustomer 整数 配音 '对数据表进行搜索比较,以生成从未出售过的客户列表.
40 For a = 0 MaxRowCustomer
41 wasFound == 如果找到匹配项,``此布尔值将设置为True
42 For b = 0 MaxRowCurrentCust
43 如果 .CustomerTableAdapter.FillByCurrentCust(( " CSCODE" .CustomerTableAdapter.FillByCustomer((( .dbSA_CUSTOMERLISTDataSet.Customer).Rows(a)( " CSCODE" ))) 然后
44 wasdFound = True
45 退出 For 这违反了"For b ="循环无需进一步检查.在当前客户表中找到了此客户公司代码.
46 结束 如果
47 下一个 b
48 如果 wasFound == 错误 然后
49 在当前客户数据表中找不到客户代码.
50 Row = dtResults.NewRow
51 dtResults.NewRow()
52 Row.Item( " CSCODE" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
53 Row.Item( " SANAME" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( &"SANAME"" Row.Item( " CSNAME" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
55 Row.Item( " CSADDR1" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( " CSADDR1" Row.Item( " CSADDR2" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
57 ``Row.Item( " CSCITY" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( &"CSCITY"" Row.Item( " CSST" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
59 " CSZIP" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( &"CSZIP"" Row.Item( " CSTYPE" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
61 Row.Item( " CSENTDATE" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( &"CSENTDATE"" Row.Item( " CSMODDATE" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( ))
63 Row.Item( " CSPHONE" )= .CustomerTableAdapter.FillByCustomer( .dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)( &"CSPHONE" dtResults.Rows.Add(Row)
65 其他
66 在当前客户数据表中找到了客户代码.
67 结束 如果
68 下一个 a
69 txtResults.Text = CStr (dtResults.Rows.Count) 在dtResults中找到显示的行数.
70 如果 txtResults.Text = " txtResults.Text = 0 然后
71 退出 Sub
72 结束 如果
73 dgResults.DataSource = dtResults 显示输出到datagridview.
74 创建用于导出为Excel文件格式的数据集".
75 博士 DataRow
76 昏暗 dsResults '将表添加到数据集.
77 dsResults.Tables.Add()
78
79 对于 i 整数 = 0 = dgResults.RowCount-1
80 dr = dsResults.Tables(0).NewRow
81 用于 j 整数 = 0 = dgResults.ColumnCount-1
82 dr(j)= dgResults.Rows(i).Cells(j).Value
83 下一个
84 dsResults.Tables(0).Rows.Add(dr) '将行添加到表中.
85 下一个
86
87 excel 新建 Microsoft.Office .Interop.Excel.Application
88 昏暗 wBook Microsoft.Office.Interop.Excel.Workbook
89 wSheet Microsoft.Office.Interop.Excel.Worksheet
90
91 wBook = excel.Workbooks.Add
92 wSheet = wBook.ActiveSheet
93
94 昏暗 dt System.Data.DataTable = dsResults.Tables(0)
95 dccol As System.Data.DataColumn
96 昏暗 卓尔 DataRow
97 colIndex As 整数 = 0
98 rowIndex 整数 For 每个 dccol excel.Cells(1,colIndex)= dccol.ColumnName
103 下一个
104
105 对于 每个 卓尔 dtResults.Rows
106 rowIndex = rowIndex + 1
107 colIndex = 0
108 对于 每个 dccol excel.Cells(rowIndex + 1,colIndex)= drow(dccol.ColumnName)
111 下一个
112
113 wSheet.Columns.AutoFit()
114 Dim strFilename <字体样式="color:blue">为 字符串 " C:\ Documents and Settingss \ All \ Users \ Desktop \ OpenCustomers.xls" Dim binFileOpen As 布尔值 == 错误
116 尝试
117 fileTemp System.IO.FileStream = System.IO.File.OpenWrite(strFilename)
118 fileTemp.Close()
119 捕获 例如 例外
120 binFileOpen = False
121 结束 尝试
122
123 如果 System.IO.File.Exists(strFilename) 然后
124 System.IO.File.Delete(strFilename)
125 结束 如果
126
127 wBook.SaveAs(strFilename)
128 excel.Workbooks.Open(strFilename)
129             excel.Visible = True 
130         Next 
131         'Me.ReportViewer1.RefreshReport()  
132     End Sub 
133     End Class 
134  
1 Option Explicit On 
2 Option Strict Off  
3  
4 Imports excel = Microsoft.Office.Interop.Excel.Application  
5 Imports wBook = Microsoft.Office.Interop.Excel.Workbook  
6 Imports wSheet = Microsoft.Office.Interop.Excel.Sheets  
7  
8 Public Class frmOpenCustomers  
9  
10     Private Sub frmOpenCustomers_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
11         'TODO: This line of code loads data into the 'dbSA_CUSTOMERLISTDataSet.Customer' table. You can move, or remove it, as needed.  
12         Me.CustomerTableAdapter.Fill(Me.dbSA_CUSTOMERLISTDataSet.Customer)  
13  
14     End Sub 
15  
16     Private Sub btnRun_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnRun.Click  
17         'Setup data table to hold results from comparison of all customers to data table of customers that have generated an invoice in system.  
18         Dim dtResults As New DataTable  
19         Dim Row As DataRow  
20         'Define columns of data for dtResults.  
21         dtResults.Columns.Add("CSCODE"GetType(String))  
22         dtResults.Columns.Add("SANAME"GetType(String))  
23         dtResults.Columns.Add("CSNAME"GetType(String))  
24         dtResults.Columns.Add("CSADDR1"GetType(String))  
25         dtResults.Columns.Add("CSADDR2"GetType(String))  
26         dtResults.Columns.Add("CSCITY"GetType(String))  
27         dtResults.Columns.Add("CSST"GetType(String))  
28         dtResults.Columns.Add("CSZIP"GetType(String))  
29         dtResults.Columns.Add("CSTYPE"GetType(String))  
30         dtResults.Columns.Add("CSENTDATE"GetType(String))  
31         dtResults.Columns.Add("CSMODDATE"GetType(String))  
32         dtResults.Columns.Add("CSPHONE"GetType(String))  
33         'define query parameters.  
34         Dim wasFound As Boolean = False 
35         Dim a As Integer = 0  
36         Dim b As Integer = 0  
37         Dim MaxRowCurrentCust As Integer = CInt(Me.CustomerTableAdapter.ScalarQueryCurrentCustNumber.Value) - 1  
38         Dim MaxRowCustomer As Integer = CInt(Me.CustomerTableAdapter.ScalarQueryCustomerCount.Value) - 1  
39         'perform search compaison of data tables to generate a list of customers that have never been sold to.  
40         For a = 0 To MaxRowCustomer  
41             wasFound = False 'This boolean will be set to True if a match is found  
42             For b = 0 To MaxRowCurrentCust  
43                 If Me.CustomerTableAdapter.FillByCurrentCust((Me.dbSA_CUSTOMERLISTDataSet.Customer).Rows(b)("CSCODE")) = Me.CustomerTableAdapter.FillByCustomer((Me.dbSA_CUSTOMERLISTDataSet.Customer).Rows(a)("CSCODE")) Then 
44                     wasFound = True 
45                     Exit For 'This breaks out of the "For b =" loop No need to check any further.  This customer's Company Code was found in the current customer table.  
46                 End If 
47             Next b  
48             If wasFound = False Then 
49                 'That customer's code was not found in current customer data table.  
50                 Row = dtResults.NewRow  
51                 dtResults.NewRow()  
52                 Row.Item("CSCODE") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSCODE"))  
53                 Row.Item("SANAME") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("SANAME"))  
54                 Row.Item("CSNAME") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSNAME"))  
55                 Row.Item("CSADDR1") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSADDR1"))  
56                 Row.Item("CSADDR2") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSADDR2"))  
57                 Row.Item("CSCITY") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSCITY"))  
58                 Row.Item("CSST") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSST"))  
59                 Row.Item("CSZIP") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSZIP"))  
60                 Row.Item("CSTYPE") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSTYPE"))  
61                 Row.Item("CSENTDATE") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSENTDATE"))  
62                 Row.Item("CSMODDATE") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSMODDATE"))  
63                 Row.Item("CSPHONE") = Me.CustomerTableAdapter.FillByCustomer(Me.dbSA_CUSTOMERLISTDataSet.Customer.Rows(a)("CSPHONE"))  
64                 dtResults.Rows.Add(Row)  
65             Else 
66                 'That customer's code was found in the curreent customer data table.  
67             End If 
68         Next a  
69         txtResults.Text = CStr(dtResults.Rows.Count) 'Display number of rows found in dtResults.  
70         If txtResults.Text = "" Or txtResults.Text = 0 Then 
71             Exit Sub 
72         End If 
73         dgResults.DataSource = dtResults  'Display output to datagridview.  
74         'Create dataset for exporting to Excel file format.  
75         Dim dr As DataRow  
76         Dim dsResults As New DataSet 'Add table to dataset.  
77         dsResults.Tables.Add()  
78  
79         For i As Integer = 0 To dgResults.RowCount - 1  
80             dr = dsResults.Tables(0).NewRow  
81             For j As Integer = 0 To dgResults.ColumnCount - 1  
82                 dr(j) = dgResults.Rows(i).Cells(j).Value  
83             Next 
84             dsResults.Tables(0).Rows.Add(dr) 'Add rows to the table.  
85         Next 
86  
87         Dim excel As New Microsoft.Office.Interop.Excel.Application  
88         Dim wBook As Microsoft.Office.Interop.Excel.Workbook  
89         Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet  
90  
91         wBook = excel.Workbooks.Add  
92         wSheet = wBook.ActiveSheet  
93  
94         Dim dt As System.Data.DataTable = dsResults.Tables(0)  
95         Dim dccol As System.Data.DataColumn  
96         Dim drow As DataRow  
97         Dim colIndex As Integer = 0  
98         Dim rowIndex As Integer = 0  
99  
100         For Each dccol In dtResults.Columns  
101             colIndex = colIndex + 1  
102             excel.Cells(1, colIndex) = dccol.ColumnName  
103         Next 
104  
105         For Each drow In dtResults.Rows  
106             rowIndex = rowIndex + 1  
107             colIndex = 0  
108             For Each dccol In dtResults.Columns  
109                 colIndex = colIndex + 1  
110                 excel.Cells(rowIndex + 1, colIndex) = drow(dccol.ColumnName)  
111             Next 
112  
113             wSheet.Columns.AutoFit()  
114             Dim strFilename As String = "C:\Documents and Settings\All Users\Desktop\OpenCustomers.xls" 
115             Dim binFileOpen As Boolean = False 
116             Try 
117                 Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFilename)  
118                 fileTemp.Close()  
119             Catch ex As Exception  
120                 binFileOpen = False 
121             End Try 
122  
123             If System.IO.File.Exists(strFilename) Then 
124                 System.IO.File.Delete(strFilename)  
125             End If 
126  
127             wBook.SaveAs(strFilename)  
128             excel.Workbooks.Open(strFilename)  
129             excel.Visible = True 
130         Next 
131         'Me.ReportViewer1.RefreshReport()  
132     End Sub 
133     End Class 
134  

推荐答案

Still Around?

I think a better approach would be to loop through the rows of customer table and then use rowfilter property on the currentcust table to see if the invoice has been sent (i am assuming that you need to compare customer table with currentcust and if a customer does not exist in currentcust then add that to resultset).

For i as integer = 0 to tblCustomer.Rows.Count - 1
     tblCurrentCust.DefaultView.RowFilter = "CustomerID = " & tblCustomer.Rows(i).item("CustomerID")
     if tblcurrentcust.DefaultView.Count = 0 Then
     'Add row to resultset
     End if
     tblCurrentCust.DefaultView.RowFilter = ""
Next
Still Around?

I think a better approach would be to loop through the rows of customer table and then use rowfilter property on the currentcust table to see if the invoice has been sent (i am assuming that you need to compare customer table with currentcust and if a customer does not exist in currentcust then add that to resultset).

For i as integer = 0 to tblCustomer.Rows.Count - 1
     tblCurrentCust.DefaultView.RowFilter = "CustomerID = " & tblCustomer.Rows(i).item("CustomerID")
     if tblcurrentcust.DefaultView.Count = 0 Then
     'Add row to resultset
     End if
     tblCurrentCust.DefaultView.RowFilter = ""
Next


这篇关于帮助两个数据表之间的比较语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆