Hi!

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

mysql_query($query);

I used to handle c# using data reader

and I wanna ask if theres other way of doing this

thanks!

Recommended Answers

All 2 Replies

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

commented: spotted mistake in my example +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.