Hi,
Following is a function I wrote to export the rows of an arbitrary
table. It's not complete yet, in that it currently does not output to
a file (it uses Debug.WriteLine () at the moment) and it currently
assumes MS-SQL Server database types (int, bit, varchar, etc) and only
handles a few of the datatypes.
It works, but it's rather slow. The problem being that every field of
every row must be checked for the datatype, so that the CSV file can
be properly formatted. Eg, text fields are single-quoted and any
single quotes in the text fields must be replaced with two single
quotes (eg, the Replace("'","'' ") call).
Does anyone know of a faster and more efficient way of accomplishing
this?
Thanks,
// CHRIS
CODE:
<NOTE>
Output is with Debug.WriteLine (), no file created yet;
also, (String) m_SQL_Connectio n must be defined for the
sqlCmd.Connecti on property setting.
</NOTE>
Public Function ExportQuery(ByV al szSQL As String, ByVal
szFilename As String) As Boolean
'Execute SQL And Save Results To CSV File
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
Dim n As Integer
Dim k As Integer
Dim nRow As Integer = 0
Dim nCols As Integer
Dim DC As DataColumn = Nothing
Dim obDateTime As DateTime
Try
sqlCmd = New SqlCommand(szSQ L)
sqlCmd.Connecti on = m_SQL_Connectio n
sqlReader = sqlCmd.ExecuteR eader()
If sqlReader.HasRo ws() = True Then
Do While sqlReader.Read( ) = True
If nRow = 0 Then
nCols = sqlReader.Field Count()
For n = 0 To nCols - 1
If n > 0 Then Debug.Write("," )
Debug.Write(Str ing.Format("""{ 0}""",
sqlReader.GetNa me(n)))
Next
Debug.WriteLine ("")
End If
nRow = nRow + 1
For n = 0 To nCols - 1
If n > 0 Then Debug.Write("," )
Select Case sqlReader.GetDa taTypeName(n)
Case "int"
Debug.Write(Str ing.Format("{0} ",
sqlReader(n)))
Case "varchar"
Debug.Write(Str ing.Format("'{0 }'",
CType(sqlReader (n), String).Replace ("'", "''")))
Case "bit"
If CType(sqlReader (n), Boolean) = True
Then
Debug.Write("1" )
Else
Debug.Write("0" )
End If
Case "datetime"
obDateTime = CType(sqlReader (n),
DateTime)
Debug.Write(Str ing.Format("'{0 :D4}-{1:D2}-{2:D2}
{3:D2}:{4:D2}:{ 5:D2}.{6:D3}'", obDateTime.Year (), obDateTime.Mont h(),
obDateTime.Day( ), obDateTime.Hour (), obDateTime.Minu te(),
obDateTime.Seco nd(), obDateTime.Mill isecond()))
Case Else
MsgBox(String.F ormat("ExportQu ery:
Column Type '{0}' Not Currently Handled",
sqlReader.GetDa taTypeName(n)))
End Select
Next
Debug.WriteLine ("")
Loop
End If
sqlReader.Close ()
sqlCmd.Dispose( )
Catch ex As Exception
Me.m_szLastErro r = ex.Message()
Debug.WriteLine (ex.Message())
Return False
End Try
Return True
End Function 0 1543 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jeremy Langworthy |
last post by:
Hi
I am trying to create a MS Excel format CSV but I can't figure out how
to get the line feed/carriage return/new record working properly.
I am nding each line/record with these characters: "\r\n" but the
Excel file just loads all the field in one row as if it doesn't
recognise these characters.
These are the headers I am using to create the file:
|
by: jenny |
last post by:
Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny
|
by: StumpY |
last post by:
HI,
I have set up a page with a form which appends data to a .csv file on my asp
server, this is to enable a limited number of users to add news threads to
this file, which contains the data in the following format
(date,headline,article);
"181003","news title","news article"
"171003","older news title","news article"
The form adds the data in reverse chronological order with newest at the top
of the list.
Now I wish to be able to write...
|
by: perchef |
last post by:
hi,
i have written this small code which allow me to unzip files using
python :
import zipfile
import os
import os.path
pathDir="/home/toto/Desktop"
|
by: Job Lot |
last post by:
I have tab delimited text file which gets populated on daily basis via
automated process. New entry is written at the bottom. I need to create a
utility which makes a copy of this file with 10 most recent entries.
When I read line using StreamReader object it starts from the top, so
looping though lines and keeping track of the line is not helpful. Is there
anyway to start reading from the bottom or if anyone could suggest some other...
| |
by: Chris Kennedy |
last post by:
How do create a dataset from scratch based on an XML schema. This will not
be filled by a dataadapter. It will be a dataset which I add rows to and
then save as an XML file. All the example depend on filling the dataset. I
have also tried creating it from the XML schema but I am having problems
manipulating it. If someone could point me to some resources, that would be
great as I can't find anything.
|
by: Jim |
last post by:
I'm a little new to the C# way of doing thisgs... Although
I've done this sort of thing before in VS 6 / MFC...
creatnig controls, ActiveX controls, etc. Creating
controls in MFC is pretty straightforward and dare I say
intuitive? (well, as intuitive as you can get w/ MFC.)
Anyhow, I am wanting to create some controls of my own w/
C#. I'm not quite sure where to start though. I created
a Windows Control library project, and I get
a...
|
by: SP |
last post by:
Please advice. I am not familiar with the concepts of windows service.
I am trying to access a xml configuration file from windows service. But it
is failing. configuration file is in the same directory from where I install
windows service. I dont want to give an absolute path to the location of
file. When I try to start windows service, it is expecting the XML file to
be in c:\windows\system32\.
Please let me know whether there is a...
|
by: Agnes |
last post by:
drBranchForm = sqldatareader
For Each drBranch In dtUserBranch
Do While drBranchForm.Read
.......... Problem (A)
Loop
Next
I found that the datarreader only loop onces , ?? How can I make it restart
the sqldatareader ???
Thanks
|
by: c676228 |
last post by:
Hi all,
I am stuck with this problem.
the code is like this:
Dim sqlreader As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following in
the watch window:
sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |