DataTable tbl = new DataTable();
using (SqlConnection conn = new SqlConnection("my connection info"))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM myTable", conn);
dataAdapter.Fill(tbl);
}
using (var connection = new SqlConnection("Server=server.url.goes.here.com;Database=db_name_goes_here;User ID=username_goes_here;Password=password_goes_here"))
{
using (var command = new SqlCommand("SELECT * FROM myTable", connection))
{
connection.Open();
using (var reader = command.ExecuteReader()){
if(reader.HasRows){
while(reader.Read()){
// do stuff
}
}
}
}
}
--EDIT--
Forgot the () after ExecuteReader
Reporting by definition is different. Otherwise we would just show it on the screen.
Thank you for taking the time to type all of that. I'm running into an issue right off the bat, however.
Compiler Error Message: CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
And I added "using System.Data.SqlClient;" at the top, but it gives me another error:
Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declaration
kid_epicurus
Member
6 Points
26 Posts
Connecting to a database, sending a query, and looping through results
Aug 09, 2012 04:33 PM|LINK
I'm familiar with PHP. What's the equivalent in ASP.net for doing the following:
<?php mysql_connect('server.url.goes.here.com', 'username_goes_here', 'password_goes_here'); mysql_select_db('db_name_goes_here'); $q = 'SELECT * FROM myTable'; $r = mysql_query($q); while (mysql_fetch_object($r)) { // do stuff } ?>ganesh.rane
Participant
1854 Points
318 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 09, 2012 04:36 PM|LINK
Hi Refer this link
Using ADO.NET for beginners
http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners#DataSet
Ganesh Rane
Don't forget to mark useful responses as Answer if they helped you towards a solution.
MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 09, 2012 04:37 PM|LINK
This is a place to start:
DataTable tbl = new DataTable(); using (SqlConnection conn = new SqlConnection("my connection info")) { SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM myTable", conn); dataAdapter.Fill(tbl); }http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
Ruchira
All-Star
42975 Points
7025 Posts
MVP
Re: Connecting to a database, sending a query, and looping through results
Aug 10, 2012 12:39 PM|LINK
Hello,
Like this
while (r.Read()) { //do stuff }So altogether
SqlConnection connection; SqlDataReader r; try { string connectionString = @ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; connection = new SqlConnection(connectionString); connection.Open(); string q = "SELECT * FROM myTable"; SqlCommand sqlComm = new SqlCommand(q, connection); r = sqlComm.ExecuteReader(); while (r.Read()) { //do stuff } } catch(Exception) {} finally { r.Close(); connection.Close(); }
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.ryanbesko
Contributor
3561 Points
619 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 11, 2012 11:12 PM|LINK
using (var connection = new SqlConnection("Server=server.url.goes.here.com;Database=db_name_goes_here;User ID=username_goes_here;Password=password_goes_here")) { using (var command = new SqlCommand("SELECT * FROM myTable", connection)) { connection.Open(); using (var reader = command.ExecuteReader()){ if(reader.HasRows){ while(reader.Read()){ // do stuff } } } } }--EDIT--
Forgot the () after ExecuteReader
ryanbesko
Contributor
3561 Points
619 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 12, 2012 03:38 AM|LINK
Missed you were looking for a MySql solution. Here you go.
using (var connection = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};Database=db_name_goes_here;Server=server.url.goes.here.com;UID=username_goes_here;PWD=password_goes_here;")) { using (var command = new OdbcCommand("SELECT * FROM myTable", connection)) { connection.Open(); using (var reader = command.ExecuteReader()){ if(reader.HasRows){ while(reader.Read()){ // do stuff } } } } }Objects created with a using (blah...) statement do not need to have .Close() called on them.
kid_epicurus
Member
6 Points
26 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 13, 2012 03:01 PM|LINK
Thank you for taking the time to type all of that. I'm running into an issue right off the bat, however.
Compiler Error Message: CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
And I added "using System.Data.SqlClient;" at the top, but it gives me another error:
Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declaration
Ruchira
All-Star
42975 Points
7025 Posts
MVP
Re: Connecting to a database, sending a query, and looping through results
Aug 13, 2012 03:14 PM|LINK
Post your code please. You seems have syntax errors in your code.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.kid_epicurus
Member
6 Points
26 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 13, 2012 04:53 PM|LINK
I added this at the top and it works fine.
ryanbesko
Contributor
3561 Points
619 Posts
Re: Connecting to a database, sending a query, and looping through results
Aug 14, 2012 01:58 PM|LINK
Please mark as answer.