ADO.NET and VB
-
I fetch a record from databse using SQL query. The result is in my command object. But i don't know that how to retrieve the values from command objects and how to iterate through it. Can any one help me ?
Dim rs As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("select * from customer")
SQLREADER = rs.ExecuteReader
rs.ExecuteNonQuery()Imtiaz Imtiaz
-
I fetch a record from databse using SQL query. The result is in my command object. But i don't know that how to retrieve the values from command objects and how to iterate through it. Can any one help me ?
Dim rs As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("select * from customer")
SQLREADER = rs.ExecuteReader
rs.ExecuteNonQuery()Imtiaz Imtiaz
From the code you have given the result will not be in the Command Object as there are no output parameters. Second, the rs.ExecuteNonQuery() won't do anything useful because your SQL is a SELECT statement. ExecuteNonQuery is for when you do not expect any results back. Your results are in the SqlDataReader object. Use the SqlDataReader.Read()[^] method to extract each record. The return value of this method will be true if a record was read, or false if there are no more records. The data reader holds tha values of the current record, it has a number of Get...[^] methods to assist you in getting the values for each of the fields. After you have the values you need for a record, loop around again calling the Read method until you have all your data. Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
-
I fetch a record from databse using SQL query. The result is in my command object. But i don't know that how to retrieve the values from command objects and how to iterate through it. Can any one help me ?
Dim rs As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("select * from customer")
SQLREADER = rs.ExecuteReader
rs.ExecuteNonQuery()Imtiaz Imtiaz
Try this if you want a data reader:
Dim cnn as SqlConnection = New Connection(connectionString) Dim cmd as SqlCommand = New SqlCommand("SELECT * FROM Customer", cnn) Dim rdr as SqlDataReader = cmd.ExecuteReader() 'Now iterate through the reader While rdr.Read() Console.WriteLine(rdr("CustomerName")) End While ...
Dan Morris