Connecting to MySQL from asp.NET
To connect to mySQL from .net 2 things are required
odbc .NET connector (available from microsoft.com)
mySQL .net adapter (available from MySQL site)
then, in c#
DECLARATIONS
using System.Data.Odbc;
CONNECTION (written from memory as a guide)
string connectionstring="Driver={mySQL};Server=data.domain.com;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;";
odbcConnection goconnect = new odbcConnection(connectionstring);
goconnect.Open();
odbcCommand insertsumat=new odbcCommand("insert into tablename values('fred',17')",goconnect);
insertsumat.ExecuteNonQuery();
odbcCommand retrievesumat=new odbcCommand("select * from users where name like 'tom'",goconnect);
odbcDataReader readit=retrievesumat.ExecuteReader();
while(readit.Read())
{
Response.Write(readit["fieldnamehere"].ToString());
}
readit.Close();
goconnect.Close();
Thats pretty much it, used in same way as a OleDB connection to an access database, just using a different adaptor (odbc) and a new connection string. Any probs check port matches that of mySQL.
odbc .NET connector (available from microsoft.com)
mySQL .net adapter (available from MySQL site)
then, in c#
DECLARATIONS
using System.Data.Odbc;
CONNECTION (written from memory as a guide)
string connectionstring="Driver={mySQL};Server=data.domain.com;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;";
odbcConnection goconnect = new odbcConnection(connectionstring);
goconnect.Open();
odbcCommand insertsumat=new odbcCommand("insert into tablename values('fred',17')",goconnect);
insertsumat.ExecuteNonQuery();
odbcCommand retrievesumat=new odbcCommand("select * from users where name like 'tom'",goconnect);
odbcDataReader readit=retrievesumat.ExecuteReader();
while(readit.Read())
{
Response.Write(readit["fieldnamehere"].ToString());
}
readit.Close();
goconnect.Close();
Thats pretty much it, used in same way as a OleDB connection to an access database, just using a different adaptor (odbc) and a new connection string. Any probs check port matches that of mySQL.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home