SQL Insert Comma Delimited string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

String = “val1, val2, va3, va4â€
Insert value into col1 (String)

I am looking for an example of an insert statement that parses a string with
multi values separated by a comma. I want to parse these values and put one
Value, per row, into the same column.

Thanks

BrianDH
 
Do you want to use a stored procedure? Otherwise you will need to parse it
in VB code and send in multiple insert statements. (At least I know of no
other way)

Chris
 
This is a one time deal. We have a string in a web form of about 45 email
address. what I want to do is parse the sting into a new table and stop
using this string and start managing these via the DB.

So yes use VB code.
 
Use the String.Split(",") to break them up into an array. Then loop through
and insert them into the DB one by one.

Chris
 
Done!

Dim emailString As String = "val1, val2, val3, val4, val5"
Dim srString() = Split(emailString, ";")
Dim intCount As Integer = srString.Length
Dim X As Integer = 0
Dim myCommand As New SqlCommand
Dim myTrans As SqlTransaction
Dim myConnString As String =
"Server=Server;Database=Database;UID=;PWD=;"
Dim myConnection As New SqlConnection(myConnString)
'Open connection
myConnection.Open()
' Start Transaction
myTrans = myConnection.BeginTransaction()
myCommand.Connection = myConnection
myCommand.Transaction = myTrans
Do Until X = intCount
myCommand.CommandText = "Insert into TableName (EmailAddress)
VALUES ('" & srString(X) & "')"
myCommand.ExecuteNonQuery()
X = X + 1
Loop
' Commit transaction
myTrans.Commit()
' Close Connection
myConnection.Close()
 
And Brian,

What was the reason (problem) in this I asked that you have sent the
question?

It are more classes which are used, so which one did give you problems?

Cor
 
Complete humbug typing

That you have sent the message = Why you have sent the message
 
Back
Top