Splitting SELECT query to different STRING variables  
Author Message
Yoni_Nijs





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

Hi, I got a question,
I execute a SELECT query on a table, but how can I save those results in different variables (one variable / column)

E.G.:
SELECT Naam, Lidnr, Adres, Voornaam, Postcode, Gemeente
FROM tblLeden
WHERE (Lidnr = )

strLidnr = queries.qryKlantIDvanUitlening(strBoeknummer)
strNaam = queries.qryTest(strLidnr)

But when printing out strNaam I will only get column "Naam", how can I do this I want something like
strNaam = queries.QryTest(strLidnr)("Naam")
strAdres = queries.qryTest(strLidnr)("Voornaam")

or something like this, so I can save my result for each column in a different variable
I Tried using a query for every column (Naam,Adres,Voornaam...) But that makes the program kinda slow :s

Greetings


.NET Development24  
 
 
Ayhan Yerli





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

Hey, Ben je uit Netherland :)

You can use a SQL.DataReader object..

I don'remember the exact syntax but it must be something like that:

CmdStr As String = "SELECT Naam, Lidnr, Adres, Voornaam, Postcode, Gemeente " & _
" FROM tblLeden WHERE Lidnr = " & YOUR_VARIABLE

Dim cmd as new SQLClient.SqlCommand (cmdStr, YOUR_CONNECTION)

Dim dr as SqlClient.SqlDataReader = cmd.ExecuteReader

While Dr.Read
StrLidnr = dr.GetString(0) 'index of the column or I don't remember very well but you can also write the colum name like dr.GetString("Lidnr")

StrAdres = dr.GetString("Adres")

StrPostCode = dr.GetString("Postcode") 'According the data type you can ask GetByte or GetDate

End While

I hope It helps..



 
 
Yoni_Nijs





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

I dont know, I can't get the name of my connection (using VS.NET 2005)
 
 
Yoni_Nijs





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

btw, I'm from belgium :p

making a program for my end work or how you say it in english


 
 
Ayhan Yerli





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

Dim connStr as String = "Server= Local; Initial Catalog = YourDataBase; UId=YouUserName; Pwd= YourPassword"

Dim conn as new SQLCLient.SqlConnection(connStr)

Conn.open()

CmdStr As String = "SELECT Naam, Lidnr, Adres, Voornaam, Postcode, Gemeente " & _
" FROM tblLeden WHERE Lidnr = " & YOUR_VARIABLE

Dim cmd as new SQLClient.SqlCommand (cmdStr, conn)

Dim dr as SqlClient.SqlDataReader = cmd.ExecuteReader

While Dr.Read
StrLidnr = dr.GetString(0) 'index of the column or I don't remember very well but you can also write the colum name like dr.GetString("Lidnr")

StrAdres = dr.GetString("Adres") ' or StrAdres= dr.getString(2)

StrPostCode = dr.GetString("Postcode") 'According the data type you can ask GetByte or GetDate

End While

Conn.close()

If you do not have a connection string how did you manage to excute the query you mentioned in your post

Are you using a typed dataset

If it is the case, than you already have your connection created, you have your typed data tables and table adapters. If it is the case you must have a table adapter called " tblLedenTableAdapter" and you must have a table called "tblLedenDataTable"

so you can use this objects like

Dim mLedenAdapter As New tblLedenTableAdapter
Dim mLedenTable As New LadenDataTable = mledenAdpater.GetData()

Dim mLedenRow as tblLedenRow

For each mLedenRow in mLedenTable.Rows

strNaam = mLedenRow.Naam
strAdres = mLedenRow.Adres
ad so on...

Next



 
 
Yoni_Nijs





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

I got it working, thx very much!

 
 
Ayhan Yerli





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

I am glad if I could help ;)



 
 
Alexey Raga





PostPosted: .NET Framework Data Access and Storage, Splitting SELECT query to different STRING variables Top

BTW, you can put your connection string into the .config file:

<configuration>
<
connectionStrings>
<
add name="historyConnectionString"
connectionString="............your connection string................"
providerName="System.Data.SqlClient" />
</
connectionStrings>

Or you can do it by mouse in your application properties -> Settings tab, add new value, set type "Connection String". This configuration will be added into your .config file automatically.

And use it after in your code:
ConfigurationManager.ConnectionStrings("historyConnectionString")

I just hate hardcoded things :)

P.S. Hello from Belgium :) But I don't know nither Dutch nor French :)