Results 1 to 4 of 4

Thread: trying to insert records into SQL database

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    2

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width