|
-
Jul 31st, 2007, 10:34 PM
#1
Thread Starter
New Member
trying to insert records into SQL database
I am new to VS 2005/SQL. Am trying to insert records into a new SQL database from a flat file. The code below executes, but nothing actually gets put into the table. I'm sure that I am missing something obvious. Thanks in advance for your help. Here's the code...
Code:
Dim AllStamps As New DataSet("StampOrganizer")
Dim workTable As DataTable = AllStamps.Tables("StampStaticInfo")
Dim Fields() As String
Dim InputFile As FileIO.TextFieldParser
Dim ConnectionString As String
Dim n%
ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Stamps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim conn As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand
cmd.Connection = conn
''''''''''''''''''''''''''''''''''''''''''
' Add rows to Table via flat .txt file
''''''''''''''''''''''''''''''''''''''''''
InputFile = My.Computer.FileSystem.OpenTextFieldParser(InFile$, vbTab)
Do While Not InputFile.EndOfData
Fields = InputFile.ReadFields
For n% = 0 To 2
Fields(n%) = Replace(Fields(n%), "'", "`")
Next
cmd.CommandText = "INSERT INTO StampStaticInfo (ScottNo,Denomination,Description)" & _
" VALUES ('" & Fields(0) & "'," & Fields(1) & "," & "'" & Fields(2) & "')"
conn.Open()
n% = cmd.ExecuteNonQuery()
conn.Close()
Loop
InputFile.Close()
Last edited by Hack; Aug 1st, 2007 at 05:35 AM.
Reason: Added Code Tags
-
Aug 1st, 2007, 07:01 AM
#2
Re: trying to insert records into SQL database
I had a similar problem and Si pointed me at a post of JMcIlhinney's. That might be your problem
edit> also, as an aside, you don't need to open and close your connection each time you run the insert. It would be more efficient to open it before the loop and close it afterwards.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Aug 15th, 2007, 06:28 AM
#3
Thread Starter
New Member
Re: trying to insert records into SQL database
Thanks for the suggestion. I found that setting & changed it as described, but still no luck. I don't think this could be the problem anyway, as I was able to manually add a record into the table & that one persisted. Since this is my first time around, perhaps it is something more basic, like perhaps I'm not connected to the correct table or some such thing.
Here's an example of what one of the cmd.CommandText looks like:
"INSERT INTO StampStaticInfo (ScottNo,Denomination,Description) VALUES ('2',0.10,'Washington - black')"
Any suggestions?
-
Aug 15th, 2007, 06:35 AM
#4
Re: trying to insert records into SQL database
Is ScottNo an AutoNumber Primary Key? IF so you do not include that in the insert statement. Is there an error number provided in feedback. Have you set brakepoints and walk trough the code a line at a time looking for errors?
Sometimes the Programmer
Sometimes the DBA
Mazz1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|