How to get nvarchar data from SQL Server database

1.5k views Asked by At

I'd like to get some values from nvarchar column (SQL Server database). My code looks like below:

Dim value1 As String

Dim con2 As New System.Data.SqlClient.SqlConnection
con2.ConnectionString = "data source=AAA;initial catalog=DWH;integrated security=SSPI;"

con2.Open()

Dim retrieveGSFdata As New System.Data.SqlClient.SqlCommand
retrieveGSFdata.Connection = con2
retrieveGSFdata.CommandText = "SELECT * FROM table1"

Dim myGSFdataReader as System.Data.SqlClient.SqlDataReader
myGSFdataReader = retrieveGSFdata.ExecuteReader()

Dim iteratorGSF As Integer
iteratorGSF = 0

while(myGSFdataReader.read())

    Select Case iteratorGSF
      case 0:
        if myGSFdataReader.IsDBNull(4)
           value1 = 0
        else
           value1 = myGSFdataReader.GetString(4)
        end if
    End Select  

    iteratorGSF = iteratorGSF + 1

End while

myGSFdataReader.Close()
con2.Close()

It works fine only when there are numeric values in the column. But in this column are stored variable characters values and this types of values are not being displayed on the page. I don't know what is the reason.

I've tried to use getValue.ToString instead of getString but it doesn't work either.

I'd be grateful for your help.

1

There are 1 answers

2
Amy On

Try

value1 = myGSFdataReader.GetValue(4)

Or

value1 = CStr(myGSFdataReader.GetValue(4))