vb.net Task.Factory multiple tasks needed? - vb.net

Is this asynchronous programming correct ?
Since this is my first time using TAP, I want to make sure I do it correctly from the beginning.
I want to fill a table from a ODBC database and afterwards read some files and extract values out of it, without freezing my UI.
Why do I need to run OdbcDataAdapter and the file reading as tasks if I run the whole Function as a task in my UI Sub ? Otherwise it blocks my UI. thread.
UI Code
Private Async Sub frmOfsList_Shown(sender As Object, e As EventArgs) Handles MyBase.Show
Dim sw As New Stopwatch 'query time
sw.Start()
DataGridView1.Visible = False
Label2.Visible = False
DataGridView1.DataSource = Await OFS.GetJobList 'async method
sw.Stop()
Label2.Text = "Query time: " & sw.Elapsed.TotalSeconds & "s"
For i As Integer = 0 To DataGridView1.Rows.Count - 1 'color days until prodution date
If DataGridView1.Rows(i).Cells(3).Value < 0 Then
DataGridView1.Rows(i).Cells(3).Style.ForeColor = Color.Red
Else
DataGridView1.Rows(i).Cells(3).Style.ForeColor = Color.Green
End If
Next
DataGridView1.Visible = True 'show grid
DataGridView1.ClearSelection()
Label2.Visible = True
End Sub
Async Function
Public Shared Async Function GetJobList() As Task(Of DataTable)
Dim dq As Char = """"
Dim con As OdbcConnection = New OdbcConnection(constr)
con.Open()
'get data from OFS
Dim cmd As String = "SELECT p1.ProductionOrder, p1.Project, p1.ProductionDate, p1.Item, p1.Revision, p1.PlannedQty FROM " &
dq & "OFS460" & dq & "." & dq & "dbo" & dq & "." & dq & "tblProductionOrders" & dq & " p INNER JOIN " & dq & "OFS460" & dq & "." & dq & "dbo" &
dq & "." & dq & "tblProductionOrders" & dq & " p1 ON p.ProductionOrder = p1.ProductionOrder WHERE (p.Task=2820 AND p.StatusID=4) AND (p1.Task=2830 AND (p1.StatusID=1 OR p1.StatusID=2 OR p1.StatusID=3)) ORDER BY p1.ProductionDate"
Dim adapter As OdbcDataAdapter = New OdbcDataAdapter(cmd, con)
Dim datatable As New DataTable("JobList")
'fil table with job data async
Await Task.Factory.StartNew(Sub()
adapter.Fill(datatable)
End Sub)
'add columns to table
datatable.Columns.Add("Length", GetType(Double))
datatable.Columns.Add("Outside Dia", GetType(Double))
Dim proddate As DateTime
datatable.Columns.Add("Days until").SetOrdinal(3)
'calculate days
For j As Integer = 0 To datatable.Rows.Count - 1
proddate = datatable(j)(2)
datatable.Rows(j)(3) = proddate.Subtract(DateTime.Now).Days
Next
'Get length and diameter for each part
Dim searchpath As String = My.Settings.g250path
Await Task.Factory.StartNew(Sub()
Dim files As String()
Dim filetext As String
For i As Integer = 0 To datatable.Rows.Count - 1
files = System.IO.Directory.GetFiles(searchpath, "*" & datatable.Rows(i)("Item") & "*") 'get file by item#
If files.Length > 0 Then
filetext = System.IO.File.ReadAllText(files(0)) 'read file
datatable.Rows(i)("Length") = ProgramManager.GetValue(filetext, "I_R872", 7).ToString 'extract values
datatable.Rows(i)("Outside Dia") = ProgramManager.GetValue(filetext, "I_R877", 7).ToString
End If
Next i
End Sub)
Return datatable
End Function

You should not use Task.Factory.StartNew with Async-Await. You should use Task.Run, instead.
And you only need to get out of the UI thread once for the "heavy work" and return when done.
Try this:
Public Shared Function GetJobList() As DataTable
Dim dq As Char = """"
Dim con As OdbcConnection = New OdbcConnection(constr)
con.Open()
'get data from OFS
Dim cmd As String = "SELECT p1.ProductionOrder, p1.Project, p1.ProductionDate, p1.Item, p1.Revision, p1.PlannedQty FROM ""OFS460"".""dbo"".""tblProductionOrders"" p INNER JOIN ""OFS460"".""dbo"".""tblProductionOrders"" p1 ON p.ProductionOrder = p1.ProductionOrder WHERE (p.Task=2820 AND p.StatusID=4) AND (p1.Task=2830 AND (p1.StatusID=1 OR p1.StatusID=2 OR p1.StatusID=3)) ORDER BY p1.ProductionDate"
Dim adapter As OdbcDataAdapter = New OdbcDataAdapter(cmd, con)
Dim datatable As New DataTable("JobList")
'fil table with job data async
adapter.Fill(datatable)
'add columns to table
datatable.Columns.Add("Length", GetType(Double))
datatable.Columns.Add("Outside Dia", GetType(Double))
Dim proddate As DateTime
datatable.Columns.Add("Days until").SetOrdinal(3)
'calculate days
For j As Integer = 0 To datatable.Rows.Count - 1
proddate = datatable(j)(2)
datatable.Rows(j)(3) = proddate.Subtract(DateTime.Now).Days
Next
'Get length and diameter for each part
Dim searchpath As String = My.Settings.g250path
Dim files As String()
Dim filetext As String
For i As Integer = 0 To datatable.Rows.Count - 1
files = System.IO.Directory.GetFiles(searchpath, "*" & datatable.Rows(i)("Item") & "*") 'get file by item#
If files.Length > 0 Then
filetext = System.IO.File.ReadAllText(files(0)) 'read file
datatable.Rows(i)("Length") = ProgramManager.GetValue(filetext, "I_R872", 7).ToString 'extract values
datatable.Rows(i)("Outside Dia") = ProgramManager.GetValue(filetext, "I_R877", 7).ToString
End If
Next i
Return datatable
End Function
And invoke it like this:
DataGridView1.DataSource = Await Task.Run(AddressOf OFS.GetJobList1)
This way, the OFS.GetJobList1 function will be scheduled to execute on a thread pool thread and, when completed, the execution will resume on the caller sub/function and the return value of Task.Run (which is the return value of OFS.GetJobList1 wrapped with a Task(Of DataTable)) will be unwrapped and assigned to DataGridView1.DataSource.

Related

VB.NET ACCESS Database birhtday request X time

I have the following problem: I want to extract from my access database the concerts that took place on day X. (Anniversary principle). I wish to release a report from this result. (The state works fine on its own. When I call it from VB without modifying the where clause) I wanted to prepare, for example, 5 states in advance. So I wrote this code:
`{'Dim DateJour As String
Dim I As Integer = 1
Dim strDB As String = CheminBase & "JH.mdb"
Dim RptNom As String
For I = 1 To 5
DateJour = I
If DateJour < 10 Then DateJour = "0" & DateJour
Dim Mois As String = Month(Date.Today)
If Mois < 10 Then Mois = "0" & Mois
Dim Annee As String = Year(Date.Today)
DateJour = DateJour & Mois & Annee
ClauseWhere = "Format([con_date],ddmm) = Format(" & _ DateAdd(Interval:=DateInterval.Day, I, Date.Today) & ",ddmm) order by con_Date DESC "
RptNom = "FICHE_JOUR_CONCERT_PLAN"
OLEOpenReport_PLAN(strDB, RptNom, AcView.acViewPreview, , ClauseWhere)
Next
MsgBox("Planification terminée")`}`
Here is the code of OLEOpenReport_PLAN
`{Private Function OLEOpenReport_PLAN(ByVal strDBName As String, ByVal strRptName As String, Optional ByVal intDisplay As Access.AcView = Access.AcView.acViewNormal, Optional ByVal strFilter As String = "", Optional ByVal strWhere As String = "") As Boolean
Dim bReturn As Boolean = True
Try
Dim objAccess As New Access.Application
objAccess.OpenCurrentDatabase(strDBName, False)
objAccess.DoCmd.OpenReport(strRptName, intDisplay, strFilter, strWhere, _ Access.AcWindowMode.acWindowNormal)
objAccess.DoCmd.OutputTo(Access.AcOutputObjectType.acOutputReport, strRptName, _ OutputFormat:="PDF", "C:\Users\" & Environment.UserName & "\AppData\Roaming\IP-Informatique _ Pourrieres\J H L Appli\Editions\Editions Planifiees\" & strRptName & "_" & DateJour & _".pdf",,,,)
objAccess.Quit(Access.AcQuitOption.acQuitSaveNone)
objAccess = Nothing
Catch ex As Exception
bReturn = False
MessageBox.Show(ex.ToString, "Erreur Automation", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Try
Return bReturn
End Function}`
At runtime I receive an "Operator absent" error at the level of the clausewhere. I also tried:
'{'ClauseWhere = "Format([con_date],ddmm) = Format(Date()+" & 1 & ",ddmm) order by con_Date DESC "'}'
which gives me the same error.
To be complete, here is the code of the state request:
'SELECT CONCERTS.CON_Date, CITIES.VIL_NOM
FROM CITIES INNER JOIN CONCERTS ON CITIES.IDVILLE = CONCERTS.IDVILLE;
`
Thank you in advance for your help.
Thierry
Assuming dates are stored as Dates...
I only know the vb end of this. Using the the Day and Month functions of Access Sql compare these to the Day and Month of your desired ann. date to get the concerts on this date in any year.
Private Function GetAnniversaries(AnnDate As Date) As DataTable
Dim dt As New DataTable
Dim selectString = "SELECT CONCERTS.CON_Date, CITIES.VIL_NOM
FROM CITIES
INNER JOIN CONCERTS ON CITIES.IDVILLE = CONCERTS.IDVILLE
Where Day(CONCERTS.CON_Date) = #Day And Month(CONCERTS.CON_Date) = #Month;"
Using cn As New OleDbConnection("Your connection string"),
cmd As New OleDbCommand(selectString, cn)
cmd.Parameters.Add("#Day", OleDbType.Integer).Value = Day(AnnDate)
cmd.Parameters.Add("#Month", OleDbType.Integer).Value = Month(AnnDate)
cn.Open()
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function
Private Sub Button1_Click() Handles Button1.Click
'To see what was returned
Dim AnniversaryDate = Now
Dim dt = GetAnniversaries(AnniversaryDate)
DataGridView1.DataSource = dt
End Sub

Variable '' is used before it has been assigned a value.

I'm trying to make a program that downloads a bunch of domains and adds them windows hosts file but I'm having a bit of trouble. I keep getting an error when I try storing them in a list. I don't get why it doesn't work.
Sub Main()
Console.Title = "NoTrack blocklist to Windows Hosts File Converter"
Console.WriteLine("Downloading . . . ")
Dim FileDelete As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt"
If System.IO.File.Exists(FileDelete) = True Then
System.IO.File.Delete(FileDelete)
End If
download()
Threading.Thread.Sleep(1000)
Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
Dim tempRead As String ' = s.ReadLine
Dim tempSplit As String() ' = tempRead.Split(New Char() {" "})
Dim i As Integer = 0
Dim tempStore As String()
s.ReadLine()
s.ReadLine()
Do Until s.EndOfStream = True
tempRead = s.ReadLine
tempSplit = tempRead.Split(New Char() {" "})
Console.WriteLine(tempSplit(0))
tempStore(i) = tempSplit(0)'The part that gives me the error
i = i + 1
Loop
Console.ReadKey()
End Sub
Sub download()
Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
'"Enter file URL"
Dim url As String = "https://quidsup.net/notrack/blocklist.php?download"
'"Enter directory"
Dim dirr As String = localDir & "/Downloads" & "/notracktemp.txt"
My.Computer.Network.DownloadFile(url, dirr)
'System.IO.File.Delete(localDir & "/notracktemp.txt")
End Sub
tempStore() has to have a size
count number of lines in file with loop, then declare it as tempStore(i) where i is the amount of lines. Here is a function that counts the lines.
Function countlines()
Dim count As Integer
Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
s.ReadLine()
s.ReadLine()
count = 0
Do Until s.EndOfStream = True
s.ReadLine()
count = count + 1
Loop
Console.WriteLine(count)
Return count
Console.ReadKey()
End Function
Then what you do is:
Dim count As Integer
count = countlines()
Dim tempStore(count) As String

SELECT Query WHERE multiple values from checkboxlist are used

I was wondering if it was possible to filter down data from a table using multiple values from a checkboxlist? (or any other way) I have a checkboxlist and a gridview and when you check on of the boxes it does show the right data in the gridview but the problem arises when I try to check multiple values. It seems to search for the first checked value and then ignores the rest. You'd think it'd be simple! Perhaps it is. Here is my attempt below.
CODE BEHIND
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default2
Inherits System.Web.UI.Page
Dim strSQL As New System.Text.StringBuilder
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim i As Integer, c As Integer = 0
Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
c += 1
If c = 1 Then
strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
Else
strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
End If
End If
Next
strSQL.Append("SELECT Project.*")
strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
strSQL.Append(" WHERE" & strParams)
FillGridView()
End If
End Sub
Private Sub FillGridView()
Dim strMyConn As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True"
Using MyConn As New SqlClient.SqlConnection(strMyConn)
MyConn.Open()
Dim cmd As New SqlClient.SqlCommand(strSQL.ToString, MyConn)
cmd.Connection = MyConn
cmd.CommandType = CommandType.Text
Try
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Using
If Me.GridView1.Visible = False Then Me.GridView1.Visible = True
Catch ex As Exception
Me.GridView1.Visible = False
End Try
End Using
End Sub
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i As Integer, c As Integer = 0
Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
c += 1
If c = 1 Then
strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
Else
strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
End If
End If
Next
If c <> 0 Then
strSQL.Append("SELECT Project.*")
strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
strSQL.Append(" WHERE" & strParams)
End If
End Sub
End Class
Refactor this section to create a WHERE IN statement so it checks to see if the value is found among any item checked
Before
Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
c += 1
If c = 1 Then
strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
Else
strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
End If
End If
Next
After
Dim params As StringBuilder = New StringBuilder()
For i = 0 To Me.CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
params.Append("'")
params.Append(CheckBoxList1.Items(i).Text)
If i < Me.CheckBoxList1.Items.Count Then
params.Append("',") // don't append a comma if it's the last item
End If
End If
Next
strSQL.Append("SELECT Project.* FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID WHERE Keyword.Keyword in (")
strSQL.Append(params.ToString()) // append comma delimited values that make up where in statement
strSQL.Append("')") // close where in statement
FillGridView()

extracting text from comma separated values in visual basic

I have such kind of data in a text file:
12343,M,Helen Beyer,92149999,21,F,10,F,F,T,T,T,F,F
54326,F,Donna Noble,92148888,19,M,99,T,F,T,F,T,F,T
99999,M,Ed Harrison,92147777,28,F,5,F,F,F,F,F,F,T
88886,F,Amy Pond,92146666,31,M,2,T,F,T,T,T,T,T
37378,F,Martha Jones,92144444,30,M,5,T,F,F,F,T,T,T
22444,M,Tom Scully,92145555,42,F,6,T,T,T,T,T,T,T
81184,F,Sarah Jane Smith,92143333,22,F,5,F,F,F,T,T,T,F
97539,M,Angus Harley,92142222,22,M,9,F,T,F,T,T,T,T
24686,F,Rose Tyler,92142222,22,M,5,F,F,F,T,T,T,F
11113,F,Jo Grant,92142222,22,M,5,F,F,F,T,T,T,F
I want to extract the Initial of the first name and complete surname. So the output should look like:
H. Beyer, M
D. Noble, F
E. Harrison, M
The problem is that I should not use String Split function. Instead I have to do it using any other way of string handling.
This is my code:
Public Sub btn_IniSurGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_IniSurGen.Click
Dim vFileName As String = "C:\temp\members.txt"
Dim vText As String = String.Empty
If Not File.Exists(vFileName) Then
lbl_Output.Text = "The file " & vFileName & " does not exist"
Else
Dim rvSR As New IO.StreamReader(vFileName)
Do While rvSR.Peek <> -1
vText = rvSR.ReadLine() & vbNewLine
lbl_Output.Text += vText.Substring(8, 1)
Loop
rvSR.Close()
End If
End Sub
You can use the TextFieldParserClass. It will parse the file and return the results directly to you as a string array.
Using MyReader As New Microsoft.VisualBasic.FileIO.
TextFieldParser("c:\logs\bigfile")
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
" is invalid. Skipping")
End Try
End While
End Using
For your wanted result, you may changed
lbl_Output.Text += vText.Substring(8, 1)
to
'declare this first
Dim sInit as String
Dim sName as String
sInit = vText.Substring(6, 1)
sName = ""
For x as Integer = 8 to vText.Length - 1
if vText.Substring(x) = "," Then Exit For
sName &= vText.Substring(x)
Next
lbl_Output.Text += sName & ", " & sInit
But better you have more than one lbl_Output ...
Something like this should work:
Dim lines As New List(Of String)
For Each s As String In File.ReadAllLines("textfile3.txt")
Dim temp As String = ""
s = s.Substring(s.IndexOf(","c) + 1)
temp = ", " + s.First
s = s.Substring(s.IndexOf(","c) + 1)
temp = s.First + ". " + s.Substring(s.IndexOf(" "c), s.IndexOf(","c) - s.IndexOf(" "c)) + temp
lines.Add(temp)
Next
The list Lines will contain the strings you need.

vb.net xls to csv with quotes?

I have a xls file, or a csv without quotes, and using vb.net need to turn it into a csv with quotes around every cell. If I open the xls/csv without quotes in MS Access, set every column to text and then export it, its in the format I need. Is there an easier way? If not, how do I do replicate this in vb.net? Thanks.
If you use the .Net OLE DB provider, you can specify the .csv formatting details in a schema.ini file in the folder your data files live in. For the 'unquoted' .csv the specs
should look like
[noquotes.csv] <-- file name
ColNameHeader=True <-- or False
CharacterSet=1252 <-- your encoding
Format=Delimited(,) <--
TextDelimiter= <-- important: no " in source file
Col1=VendorID Integer <-- your columns, of course
Col2=AccountNumber Char Width 15
for the 'quoted' .csv, just change the name and delete the TextDelimiter= line (put quotes around text fields is the default).
Then connect to the Text Database and execute the statement
SELECT * INTO [quotes.csv] FROM [noquotes.csv]
(as this creates quotes.csv, you may want to delete the file before each experimental run)
Added to deal with "Empty fields must be quoted"
This is a VBScript demo, but as the important things are the parameters for .GetString(), you'll can port it to VB easily:
Dim sDir : sDir = resolvePath( "§LibDir§testdata\txt" )
Dim sSrc : sSrc = "noquotes.csv"
Dim sSQL : sSQL = "SELECT * FROM [" & sSrc & "]"
Dim oTxtDb : Set oTxtDb = New cADBC.openDb( Array( "jettxt", sDir ) )
WScript.Echo goFS.OpenTextFile( goFS.BuildPath( sDir, sSrc ) ).ReadAll()
Dim sAll : sAll = oTxtDb.GetSelectFRO( sSQL ).GetString( _
adClipString, , """,""", """" & vbCrlf & """", "" _
)
WScript.Echo """" & Left( sAll, Len( sAll ) - 1 )
and output:
VendorID;AccountNumber;SomethingElse
1;ABC 123 QQQ;1,2
2;IJK 654 ZZZ;2,3
3;;3,4
"1","ABC 123 QQQ","1,2"
"2","IJK 654 ZZZ","2,3"
"3","","3,4"
(german locale, therefore field separator ; and decimal symbol ,)
Same output from this VB.Net code:
Imports ADODB
...
Sub useGetString()
Console.WriteLine("useGetString")
Const adClipString As Integer = 2
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim sAll As String
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=M:\lib\kurs0705\testdata\txt\;" _
& "Extended Properties=""text;"""
cn.Open()
rs = cn.Execute("SELECT * FROM [noquotes.csv]")
sAll = rs.GetString( adClipString, , """,""", """" & vbCrLf & """", "" )
cn.Close()
sAll = """" & Left( sAll, Len( sAll ) - 1 )
Console.WriteLine( sAll )
End Sub
Check out the method at this link.
What you can do to make sure quotes go around is append quotes to the beginning and end of each column data in the loop that is putting the column data in the file.
for example make the loop like this:
For InnerCount = 0 To ColumnCount - 1
Str &= """" & DS.Tables(0).Rows(OuterCount).Item(InnerCount) & ""","
Next
Public Class clsTest
Public Sub Test
Dim s as string = "C:\!Data\Test1.csv"
Dim Contents As String = System.IO.File.ReadAllText(s)
Dim aryLines As String() = Contents.Split(New String() { Environment.Newline }, StringSplitOptions.None)
Dim aryParts() As String
Dim aryHeader() As String
Dim dt As System.Data.DataTable
For i As Integer = 0 To aryLines.Length - 1
aryParts = SplitCSVLine(aryLines(i))
If dt Is Nothing And aryHeader Is Nothing Then
aryHeader = CType(aryParts.Clone, String())
ElseIf dt Is Nothing And aryHeader IsNot Nothing Then
dt = DTFromStringArray(aryParts, 1000, "", aryHeader)
Else
DTAddStringArray(dt, aryParts)
End If
Next
dt.dump
End Sub
Public Shared Function SplitCSVLine(strCSVQuotedLine As String) As String()
Dim aryLines As String() = strCSVQuotedLine.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Dim aryParts As String() = Nothing
For i As Integer = 0 To aryLines.Length - 1
Dim regx As New Text.RegularExpressions.Regex(",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))")
aryParts = regx.Split(aryLines(i))
For p As Integer = 0 To aryParts.Length - 1
aryParts(p) = aryParts(p).Trim(" "c, """"c)
Next
Next
Return aryParts
End Function
Public Shared Function DTFromStringArray(ByVal aryValues() As String, Optional ByVal intDefaultColumnWidth As Integer = 255, Optional ByVal strTableName As String = "tblArray", Optional ByVal aryColumnNames() As String = Nothing) As DataTable
If String.IsNullOrWhiteSpace(strTableName) Then strTableName = "tblArray"
Dim dt As DataTable = New DataTable(strTableName)
Dim colNew(aryValues.GetUpperBound(0)) As DataColumn
If aryColumnNames Is Nothing Then
ReDim aryColumnNames(aryValues.Length)
Else
If aryColumnNames.GetUpperBound(0) < aryValues.GetUpperBound(0) Then
ReDim Preserve aryColumnNames(aryValues.Length)
End If
End If
For x As Integer = aryColumnNames.GetLowerBound(0) To aryColumnNames.GetUpperBound(0)
If String.IsNullOrWhiteSpace(aryColumnNames(x)) Then
aryColumnNames(x) = "Field" & x.ToString
Else
aryColumnNames(x) = aryColumnNames(x)
End If
Next
For i As Integer = 0 To aryValues.GetUpperBound(0)
colNew(i) = New DataColumn
With colNew(i)
.ColumnName = aryColumnNames(i) '"Value " & i
.DataType = GetType(String)
.AllowDBNull = False
.DefaultValue = ""
.MaxLength = intDefaultColumnWidth
.Unique = False
End With
Next
dt.Columns.AddRange(colNew)
Dim pRow As DataRow = dt.NewRow
For i As Integer = aryValues.GetLowerBound(0) To aryValues.GetUpperBound(0)
pRow.Item(i) = aryValues(i)
Next
dt.Rows.Add(pRow)
Return dt
End Function
Public Shared Sub DTAddStringArray(ByRef dt As DataTable, ByVal aryRowValues() As String)
Dim pRow As DataRow
pRow = dt.NewRow
For i As Integer = aryRowValues.GetLowerBound(0) To aryRowValues.GetUpperBound(0)
pRow.Item(i) = aryRowValues(i)
Next
dt.Rows.Add(pRow)
End Sub
End Class