Showing posts with label write. Show all posts
Showing posts with label write. Show all posts

Friday, March 30, 2012

importing XML/ASCII files to SQl database using VB express

Hi everyone,

I have to write a program in VB to receive the read data from a RFID reader for my graduation project.The problem is I am not a computer science student so I have only general info on programming.

I created my DB in VB express but I couldn't find out how to send the read data (that will be either in XML or ASCII format) to my database...The read data will be transferred to my computer by the RFID reader's software but after that I don'T know how to transfer it to my DB.As I know I have to use commands like read.xml etc,but no idea how write the complete program.

I checked the forum and couldn't find the answer,sorry if someone already answered my question and I missed it.

Thanks...

Can

i suspect that you will find greater success if you post your question here: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=38&SiteID=1|||

ok,thanks,now I 'll do it...

Can

Wednesday, March 21, 2012

Importing from a text file

I am extremly new to the SQL serve environment and am trying to import data
from a text file to a table. I figured out how to write an import query and
make table query but I can't seem to be able to figure out how to import dat
a
from a plain old text file. The fields in the query should be something
similiar to the fields in the make table query. Any suggestions?
CREATE TABLE REP
(REP_NUM CHAR(2) PRIMARY KEY,
LAST_NAME CHAR(15) NOT NULL,
FIRST_NAME CHAR(15) NOT NULL,
STREET CHAR(15),
CITY CHAR(15),
STATE CHAR(2),
ZIP CHAR(5),
COMMISSION DECIMAL(7,2),
RATE DECIMAL(3,2));Darrin
It is just an example. I hope you will get an idea
CREATE TABLE TmpStList
(
stFName varchar (10) NOT NULL,
stLName varchar (10) NOT NULL,
stEmail varchar (30) NOT NULL
)
go
The data file (hawk.dat):
"Kelly","Reynold","kelly@.reynold.com"
"John","Smith","bill@.smith.com"
"Sara","Parker","sara@.parker.com"
The format file (hawk.bcp):
8.0
4
1 SQLCHAR 0 1 "\"" 0 first_quote ""
2 SQLCHAR 0 10 "\",\"" 1 stFName ""
3 SQLCHAR 0 10 "\",\"" 2 stLName ""
4 SQLCHAR 0 30 "\"\r\n" 3 stEmail ""
bulk insert TmpStList from 'C:\Staging\hawk.dat'
with (formatfile = 'C:\Staging\hawk.bcp')
select * from TmpStList
"Darrin" <Darrin@.discussions.microsoft.com> wrote in message
news:0C357973-085A-4233-B005-8361E0686B69@.microsoft.com...
>I am extremly new to the SQL serve environment and am trying to import data
> from a text file to a table. I figured out how to write an import query
> and
> make table query but I can't seem to be able to figure out how to import
> data
> from a plain old text file. The fields in the query should be something
> similiar to the fields in the make table query. Any suggestions?
> CREATE TABLE REP
> (REP_NUM CHAR(2) PRIMARY KEY,
> LAST_NAME CHAR(15) NOT NULL,
> FIRST_NAME CHAR(15) NOT NULL,
> STREET CHAR(15),
> CITY CHAR(15),
> STATE CHAR(2),
> ZIP CHAR(5),
> COMMISSION DECIMAL(7,2),
> RATE DECIMAL(3,2));

Monday, March 19, 2012

Importing files with unknown names

I want to write a DTS that will import a file every day. The problem is that the files is not named the same thing every day. There is a naming convention (SOMMDDYY.TRN) that it will follow. I want to import this file (which is a fixed width file) each day to a table (The table will be empty each day).

After it is imported, I want to look at the NAME of the file, and pull out the date portion of it. So, if the file is called SO122603.TRN, i want to pull out 122603, and then update my table with that date for every record. So when I am done, I will have a table that represents the file I imported, with one added column. This added column would be a Date/Time that has the date that was in the filename. How do I do this?I should add though - that this file that I want to import will be in a directory BY ITSELF. (There will be no other files in that folder). Is there a way to look in a directory, see what file is there, import it, and then parse the filename so I can get the date?|||as a matter of fact...just finished building that..

Select @.Command_String = 'Dir ' + @.FilePath + '\*.*'

Insert Into XLAT_Folder exec master..xp_cmdshell

PRINT 'DISPLAY XLAT_Folder'

SELECT * FROM XLAT_Folder

Delete From XLAT_Folder_Parsed

PRINT 'PARSE FOLDER'

Insert Into XLAT_Folder_Parsed (Create_Time, File_Size, [File_Name] )
Select Convert(datetime,Substring(dir_output,1,8)
+ ' '
+ (Substring(dir_output,11,5)
+ Case When Substring(dir_output,16,1) = 'a' Then ' AM' Else ' PM' End)) As Create_Time
, Convert(Int,LTrim(RTrim(Replace(Substring(dir_outp ut,17,22),',','')))) As File_Size
, Substring(dir_output,40,(Len(dir_output)-39)) As [File_Name]
From XLAT_Folder
Where Substring(dir_output,1,1) <> ' '
And (Substring(dir_output,1,1) <> ' '
And Substring(dir_output,25,5) <> '<DIR>')
AND Substring(dir_output,40,(Len(dir_output)-39)) LIKE 'XLAT%'|||How bout adding and an Active X script Task that gets the file name and sets a global variable in the DTS package. Somewhere in the script just parse out the date or whatever.

'************************************************* *********************
' Visual Basic ActiveX Script
'************************************************* ***********************

Function Main()

Dim objFSO
Dim objFolder
Dim objFile
Dim oPKG
Dim fileName
Dim filePath
Dim oConnectionSource

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("\\server\data$\")

For Each objFile in objFolder.Files

filePath = objFile.Path
fileName = objFSO.GetFileName(objFile.Path)

Set oPKG = DTSGlobalVariables.Parent

oPKG.GlobalVariables("fileName").Value = (filePath & fileName)
Set oConnectionSource = oPKG.Connections("Text File(Source)")
oConnectionSource.DataSource = (filePath & fileName)
Set oConnectionSource = Nothing
Set oPKG = Nothing


Next

Main = DTSTaskExecResult_Success

End Function|||I prefer using the fso object since xp_cmdshell presents problems.|||Yup|||Originally posted by rnealejr
I prefer using the fso object since xp_cmdshell presents problems.

Like what?|||Memory Leaks|||Originally posted by SHICKS
Memory Leaks

In stored procedures?|||Most production sql servers drop xp_cmdshell because of security risks.|||http://www.databasejournal.com/features/mssql/article.php/1580041

This does not address memory leaks, but address issues of SP3 and XP_CMDSHELL.|||Originally posted by SHICKS
Memory Leaks

Seems to be the other way around...