Solved

Need to split column full of data  and insert to new table?

Posted on 2007-12-06
9
212 Views
Last Modified: 2013-11-26
I try this code but not working pls help me out.

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Public Class Form1
    Dim InsrSql As String
    Dim RPID As String
    Dim tempStr As String
    Dim Bname() As String
    Dim words() As String
    Dim i As Integer
    Dim pos As String
    Dim s As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Sample data in Authors
        's = "Xiao, J & Zhang, Y & Fuller, A & Xiaohua, J"

        Dim conn As New ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim SqlStr As String
        conn.Open(" Provider=SQLOLEDB; server=PQDEV1; Initial Catalog=ndev; User ID=*****;Password=****;")
        SqlStr = "SELECT  USQ_RPID ,Authors FROM Author "
        rs = conn.Execute(SqlStr, , )

        While Not rs.EOF

            RPID = rs("USQ_RPID")
            s = rs("Authors")
            words = Split(s, "&")

            For i = 0 To UBound(words)
                pos = CStr(i + 1)
                tempStr = words(i)
                Bname = Split(tempStr, ",")

                If Len(tempStr) > 0 Then

                    'MsgBox("Fname -" + Bname(0) + " L int -" + Bname(1) + " Pos -" + CStr(1 + i))

                    InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0) & "', '" & Bname(1) & "', '" & pos & "'"
                    ' rs = conn.Execute(InsrSql, , )

                End If
            Next
            rs.MoveNext()
        End While
        conn.Close()
    End Sub

   
   
End Class
0
Comment
Question by:noelroshan
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20424769
missing end )

                    InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0) & "', '" & Bname(1) & "', '" & pos & "')"
0
 

Author Comment

by:noelroshan
ID: 20424897
s = rs("Authors").Value is saying error Conversion from type 'DBNull' to type 'String' is not valid
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20424906
s = Convert.ToString(rs("Authors").Value)

However, instead of NULL in the db field, you will end up with '' (empty string)
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:noelroshan
ID: 20425027

rs.MoveNext()
Error Operation is not allowed when the object is closed.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20425042
change

rs = conn.Execute(InsrSql, , )

to

conn.Execute(InsrSql, , )

otherwise you're tramping through the previous rs= asignment
0
 

Author Comment

by:noelroshan
ID: 20425090
what is that mean "Line 1: Incorrect syntax near 'Brien'.""
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20425127
InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0).Replace("'","''") & "', '" & Bname(1).Replace("'","''") & "', '" & pos & "'"
0
 

Author Comment

by:noelroshan
ID: 20425273
InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0).Replace("'","''") & "', '" & Bname(1).Replace("'","''") & "', '" & pos & "'"

Error :Index was outside the bounds of the array.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20425357
That happens when you have a name with no comma, which causes Split to return only 1 value for Bname...
make this minor change

                tempStr = words(i)
                if InStr(tempStr, ",") < 1 then tempStr = tempStr + ","
                Bname = Split(tempStr, ",")
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question