oakley09 Moderators
Posts : 26 Reputation : 1 Join date : 2012-06-24
| Subject: SQL and c# Fri Jun 29, 2012 9:28 am | |
| Hello Mate
I am trying to connect to a sql database to get information then display the data that I want. Can anyone explain to me how that is done in c#.
| |
|
ace1234 Bronze Marketer
Posts : 59 Reputation : 5 Join date : 2012-06-24
| Subject: Re: SQL and c# Fri Jun 29, 2012 9:30 am | |
| Try this, it uses the ByteFX MySQL client. - Spoiler:
using System; using System.Data; using ByteFX.Data.MySqlClient; public class Test { public static void Main(string[] args) { string connectionString = "Server=SERVER;" + "Database=DBNAME;" + "User ID=USERID;" + "Password=PASSWORD;"; IDbConnection dbcon; dbcon = new MySqlConnection(connectionString); dbcon.Open(); IDbCommand dbcmd = dbcon.CreateCommand();
string sql = "SELECT firstname, lastname " + "FROM test"; dbcmd.CommandText = sql; IDataReader reader = dbcmd.ExecuteReader(); while(reader.Read()) { string FirstName = (string) reader["firstname"]; string LastName = (string) reader["lastname"]; Console.WriteLine("Name: " + FirstName + " " + LastName); } // clean up reader.Close(); reader = null; dbcmd.Dispose(); dbcmd = null; dbcon.Close(); dbcon = null; } }
| |
|