c# im combination with mysql

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello im totally new to c# just started of yesterday and i have this question <br/>
I already suceeded to put data in a database using c# what i now want do do is
get information out of the database and put it in a var!

I want to do something like this: This is how i did it in php!
(php) ------<br/>
<span style="white-space:pre $commando2="SELECT * FROM `leden`";<br/>
<span style="white-space:pre $resultaat2=mysql_query($commando2);<br/>
<span style="white-space:pre while($array=mysql_fetch_array($resultaat2)){<br/>
<span style="white-space:pre $email2=$array;<span style="white-space:pre
}
----
and this : (php)<br/>
<br/>

$commando3="SELECT * FROM `leden` WHERE id=$memberid "; <br/>
<span style="white-space:pre $resultaat3=mysql_query($commando3);<br/>
<span style="white-space:pre $array=mysql_fetch_array($resultaat3);<br/>
<span style="white-space:pre $home=$array[home];<br/>
<span style="white-space:pre $sex=$array[sex];<br/>
$active=$array[active];

-----
I used this line to put information in the sql database
private void Form1_Load(object sender, EventArgs e)<br/>
{ sqlClient.Insert("namen", "id,age,naam", "NULL,44,wayne"); }


I dont know how to do this with c#
I have downloaden my c# mysql settup class from a website and it goes as follows:
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using MySql.Data.MySqlClient;<br/>
<br/>
namespace MySQLClass<br/>
{<br/>
<br/>
//Dont forget to add the MySQL.Data dll to your projects references<br/>
//It can be downloaded for free from MySQLs official website.<br/>
//Link to the .NET Connector (.dll file) http://dev.mysql.com/downloads/connector/net/<br/>
<br/>
<br/>
class MySQLClient<br/>
{<br/>
MySqlConnection conn = null;<br/>
<br/>
<br/>
#region Constructors<br/>
public MySQLClient(string hostname, string database, string username, string password)<br/>
{<br/>
conn = new MySqlConnection("host=" + hostname + ";database=" + database +";username=" + username +";password=" + password +";");<br/>
}<br/>
<br/>
public MySQLClient(string hostname, string database, string username, string password, int portNumber)<br/>
{<br/>
conn = new MySqlConnection("host=" + hostname + ";database=" + database + ";username=" + username + ";password=" + password + ";port=" + portNumber.ToString() +";");<br/>
}<br/>
<br/>
public MySQLClient(string hostname, string database, string username, string password, int portNumber, int connectionTimeout)<br/>
{<br/>
conn = new MySqlConnection("host=" + hostname + ";database=" + database + ";username=" + username + ";password=" + password + ";port=" + portNumber.ToString() + ";Connection Timeout=" + connectionTimeout.ToString()
+";");<br/>
}<br/>
#endregion<br/>
<br/>
#region Open/Close Connection<br/>
private bool Open()<br/>
{<br/>
//This opens temporary connection<br/>
try<br/>
{<br/>
conn.Open();<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
//Here you could add a message box or something like that so you know if there were an error.<br/>
return false;<br/>
}<br/>
}<br/>
<br/>
private bool Close()<br/>
{<br/>
//This method closes the open connection<br/>
try<br/>
{<br/>
conn.Close();<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
return false;<br/>
}<br/>
}<br/>
#endregion<br/>
<br/>
public void Insert(string table, string column, string value)<br/>
{<br/>
//Insert values into the database.<br/>
<br/>
//Example: INSERT INTO names (name, age) VALUES(John Smith, 33)<br/>
//Code: MySQLClient.Insert("names", "name, age", "John Smith, 33");<br/>
string query = "INSERT INTO " + table + " (" + column + ") VALUES (" + value + ")";<br/>
<br/>
try<br/>
{<br/>
if (this.Open())<br/>
{<br/>
//Opens a connection, if succefull; run the query and then close the connection.<br/>
<br/>
MySqlCommand cmd = new MySqlCommand(query, conn);<br/>
<br/>
cmd.ExecuteNonQuery();<br/>
this.Close();<br/>
}<br/>
}<br/>
catch { }<br/>
return;<br/>
}<br/>
<br/>
public void Update(string table, string SET, string WHERE)<br/>
{<br/>
//Update existing values in the database.<br/>
<br/>
//Example: UPDATE names SET name=Joe, age=22 WHERE name=John Smith<br/>
//Code: MySQLClient.Update("names", "name=Joe, age=22", "name=John Smith");<br/>
string query = "UPDATE " + table + " SET " + SET + " WHERE " + WHERE + "";<br/>
<br/>
if (this.Open())<br/>
{<br/>
try<br/>
{<br/>
//Opens a connection, if succefull; run the query and then close the connection.<br/>
<br/>
MySqlCommand cmd = new MySqlCommand(query, conn);<br/>
cmd.ExecuteNonQuery();<br/>
this.Close();<br/>
}<br/>
catch { this.Close(); }<br/>
}<br/>
return;<br/>
}<br/>
<br/>
public void Delete(string table, string WHERE) <br/>
{<br/>
//Removes an entry from the database.<br/>
<br/>
//Example: DELETE FROM names WHERE name=John Smith<br/>
//Code: MySQLClient.Delete("names", "name=John Smith");<br/>
string query = "DELETE FROM " + table + " WHERE " + WHERE + "";<br/>
<br/>
if (this.Open())<br/>
{<br/>
try<br/>
{<br/>
//Opens a connection, if succefull; run the query and then close the connection.<br/>
<br/>
MySqlCommand cmd = new MySqlCommand(query, conn);<br/>
cmd.ExecuteNonQuery();<br/>
this.Close();<br/>
}<br/>
catch { this.Close(); }<br/>
}<br/>
return;<br/>
}<br/>
<br/>
public Dictionary<string, string> Select(string table, string WHERE)<br/>
{<br/>
//This methods selects from the database, it retrieves data from it.<br/>
//You must make a dictionary to use this since it both saves the column<br/>
//and the value. i.e. "age" and "33" so you can easily search for values.<br/>
<br/>
//Example: SELECT * FROM names WHERE name=John Smith<br/>
// This example would retrieve all data about the entry with the name "John Smith"<br/>
<br/>
//Code = Dictionary<string, string> myDictionary = Select("names", "name=John Smith");<br/>
//This code creates a dictionary and fills it with info from the database.<br/>
<br/>
string query = "SELECT * FROM " + table + " WHERE " + WHERE + "";<br/>
<br/>
Dictionary<string, string> selectResult = new Dictionary<string, string>();<br/>
<br/>
if (this.Open())<br/>
{<br/>
MySqlCommand cmd = new MySqlCommand(query, conn);<br/>
MySqlDataReader dataReader = cmd.ExecuteReader();<br/>
<br/>
try<br/>
{<br/>
while (dataReader.Read())<br/>
{<br/>
<br/>
for (int i = 0; i < dataReader.FieldCount; i++)<br/>
{<br/>
selectResult.Add(dataReader.GetName(i).ToString(), dataReader.GetValue(i).ToString());<br/>
}<br/>
<br/>
}<br/>
dataReader.Close();<br/>
}<br/>
catch { }<br/>
this.Close();<br/>
<br/>
return selectResult;<br/>
}<br/>
else<br/>
{<br/>
return selectResult;<br/>
}<br/>
}<br/>
<br/>
public int Count(string table)<br/>
{<br/>
//This counts the numbers of entries in a table and returns it as an integear<br/>
<br/>
//Example: SELECT Count(*) FROM names<br/>
//Code: int myInt = MySQLClient.Count("names");<br/>
<br/>
string query = "SELECT Count(*) FROM " + table + "";<br/>
int Count = -1;<br/>
if (this.Open() == true)<br/>
{<br/>
try<br/>
{<br/>
MySqlCommand cmd = new MySqlCommand(query, conn);<br/>
Count = int.Parse(cmd.ExecuteScalar() + "");<br/>
this.Close();<br/>
}<br/>
catch { this.Close(); }<br/>
return Count;<br/>
}<br/>
else<br/>
{<br/>
return Count;<br/>
}<br/>
}<br/>
}<br/>
}<br/>

<br/>

<br/>


[url=http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ef37eb19-3c77-4dc5-aa6c-917a6da7fdf2]View the full article[/url]
 
Back
Top