Im kinda new to c# devt

I used to do a php project

and Im wondering if theres anyway sql commands in php have comparable commands in c#

for example

if I wanna read a data

in php I use

$query = "SELECT * FROM table";

then execute that by


I used to handle c# using data reader

and I wanna ask if theres other way of doing this


5 Years
Discussion Span
Last Post by thines01

Yeah it's not too difficult to get the hang of in C# if you've done it in another language reading data is just the

This is to use the SQL DLL:

            using System.Data.SqlClient;

and this is the code to read the data:

            string sql = "SELECT * FROM MyTable";

            //creates new Sql command using sql statement and DB connection method
            SqlCommand cmd = new SqlCommand(sql, db.Connect());

            //creates DataReader to read selected data from database
            SqlDataReader reader;

            //stores result of SQL command and stores it within reader variable
            reader = cmd.ExecuteReader();

            //This section of code will loop while there is data to read 
            while (reader.Read())
                //code to store read data goes here

Hope this helps.


@anthonyjpv, there is also the http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.aspx.
None of this will seem as simple as the constructs in PHP or other dynamic languages, but they can be very powerful when you start doing more complex operations. And, yes, you can attach all of the data-centric classes to MySQL (download the .net connector from MySql.com).

@ChrisHunter: remmeber to close your IDataReader

Votes + Comments
spotted mistake in my example
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.