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!

Dani AI

Generated

As noted, moving from PHP's compact helpers to C# often exposes more explicit ADO.NET boilerplate. demonstrated the manual approach and pointed out the disconnected DataAdapter pattern. For less typing and clearer mapping there are two practical paths that avoid repeating low-level read loops.

A micro-ORM (for example, Dapper) keeps control over SQL while mapping rows to objects with minimal code and near-raw performance. A full ORM (Entity Framework Core) provides LINQ queries, change-tracking and migrations, which speeds model-driven work but requires more setup. Choose the micro-ORM route for small utilities and the full ORM for richer domain models.

Recommended practices regardless of approach: use parameterized queries to prevent SQL injection; keep connection strings in configuration, not inline; use language disposal patterns so connections/commands/readers are always closed; prefer async APIs in UI or high-concurrency code; pick a maintained MySQL ADO.NET provider via NuGet (there are multiple options). Centralize data access behind a small helper or repository to avoid repeating SQL/connection logic across the app.

Example (Dapper-style, minimal mapping):

using (var conn = new MySqlConnector.MySqlConnection(connString))
{
    var people = conn.Query<Person>(
        "SELECT Id, Name FROM Person WHERE Role = @role",
        new { role = "admin" }
    );
}

Common pitfalls: wrong connection string or credentials, insufficient DB privileges, column/property name mismatches (use aliases or explicit mapping), and forgetting to dispose resources. Enabling SQL logging or using a DB profiler quickly reveals whether the SQL sent and parameters match expectations.

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.

, there is also the .
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).

: 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.