944,118 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 630
  • VB.NET RSS
Oct 22nd, 2009
0

Store data in variables

Expand Post »
Hi Guys,

I am using sql in and am stuck. after "selecting * from table", how do i store whatever I have obtained from my database in a variable, for display?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SecurExpert is offline Offline
24 posts
since Sep 2009
Oct 22nd, 2009
0
Re: Store data in variables
Post the code where you are executing the select * from table . There are a number of ways to go about this.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 22nd, 2009
0
Re: Store data in variables
OK, am having something like this:
VB.NET Syntax (Toggle Plain Text)
  1. .............................
  2. .............................
  3. Dim SQLConn As New SqlConnection
  4. Dim SQLCmd As New SqlCommand
  5. SQLConn.ConnectionString = ConnStr
  6. SQLConn.Open()
  7. SQLStr = "use Institution" 'This is my database
  8. SQLCmd.CommandText = SQLStr
  9. SQLCmd.Connection = SQLConn
  10. SQLCmd.ExecuteNonQuery()
  11. SQLStr = "Select * from Stud_Details where 1"
  12. SQLCmd.CommandText = SQLStr
  13. SQLCmd.Connection = SQLConn
  14. SQLCmd.ExecuteNonQuery() 'The problem begins here! Is it OK?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SecurExpert is offline Offline
24 posts
since Sep 2009
Oct 22nd, 2009
0
Re: Store data in variables
No .. that isn't ok. You need to load the results in to a DataTable using a SqlDataReader created by calling SqlCommand.ExecuteReader() .

Example:
VB.NET Syntax (Toggle Plain Text)
  1. private void button3333_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice (NOLOCK)";
  5.  
  6. DataTable dt;
  7.  
  8. using (SqlConnection conn = new SqlConnection(connStr))
  9. {
  10. conn.Open();
  11. using (SqlCommand cmd = new SqlCommand(query, conn))
  12. {
  13. using (SqlDataReader dr = cmd.ExecuteReader())
  14. {
  15. dt = new DataTable();
  16. dt.Load(dr); //This loads a table
  17. }
  18. }
  19. conn.Close();
  20. }
  21.  
  22. MessageBox.Show(string.Format("You have {0:F0} rows", dt.Rows.Count));
  23. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 23rd, 2009
0
Re: Store data in variables
thanks alot, it just worked.
thanks for your help.

regards
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SecurExpert is offline Offline
24 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Setup Project - shortcut options?
Next Thread in VB.NET Forum Timeline: VB.Net Networking Heartbeat





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC