Two Simple Code Examples for C Connection to MySQL
- 2021-12-21 04:45:01
- OfStack
Implementation code 1. The use of mysql with its own driver installation 1 can be
This is a simple example.
There is a problem here: If dataset does not have a primary key, it may cause some logarithmic library operation problems, such as updata errors.
static void Main(string[] args)
{
string sqlstr = "select * from manavatar";
MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString);
DBConn.Open();
//MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn);
MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn);
MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx();
MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn);
DataSet myDataSet = new DataSet();
DTAdapter.Fill(myDataSet,"manavatar");
try
{
while (DBReader.Read())
{
//Console.WriteLine("11");
Console.WriteLine("DBReader:{0},\t\t\tddddd:{1},\t\t {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));
}
Console.WriteLine("0000");
}
catch (Exception e)
{
Console.WriteLine(" Failed to read! "+e.ToString());
}
finally
{
Console.WriteLine("DBReader Shut down ");
Console.WriteLine("DBConn Shut down ");
DBReader.Close();
//DBConn.Close();
}
for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)
{
Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]);
}
}
Method 2.
Post a sample code. It is very suitable for beginners.
C # Access mysql
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace SybaseUtilTest
{
class Program
{
// http://bugs.mysql.com/47422, Interested friends , You can look at this bug What's going on
static void testDataAdapter()
{
try
{
MySqlClientFactory factory = MySqlClientFactory.Instance;
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = string.Format("server={0};user id={1}; password={2}; database={3}; port={4}; pooling=false",
"localhost", "root", "passwd", "test", 3306);
conn.Open();
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = conn.CreateCommand();
da.SelectCommand.CommandText = "select * from t12345";
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText = "delete from t12345 where id = @id";
DbParameter param = factory.CreateParameter();
param.ParameterName = "@id";
param.DbType = DbType.Int32;
param.SourceColumn = "id";
param.SourceVersion = DataRowVersion.Current;
da.DeleteCommand.Parameters.Add(param);
da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
DataTable dt = new DataTable("t12345");
da.Fill(dt);
int index = 0;
foreach ( DataRow o in dt.Rows )
{
if (o["id"].Equals(4))
{
Console.WriteLine(String.Format("index={0}, to delete id = 4, col2 = {1}" , index, o["col2"]));
break;
}
index++;
}
dt.Rows[index].Delete();
da.Update(dt);
dt.AcceptChanges();
da.Dispose();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Source + " "
+ ex.Message + " "
+ ex.StackTrace);
}
}
static void Main(string[] args)
{
testDataAdapter();
}
}
}
The above is this site this site for everyone to sort out the c # connection mysql database method, the need for friends can refer to 1.