#region-----------------------MySql, 数据库操作 示例-----------------------------------------------
///
/// MySql 数据库操作 using MySQLDriverCS
///
public static void MySQLDemo()
{
// string mySqlConnStr = "server = localhost;database = database; login = root; pass = 123456;port = 3306";
Console.WriteLine("-----------------Mysql 连接--------------------------------");
MySQLConnection mySQLConn = new MySQLConnection(new MySQLConnectionString("localhost", "database", "root", "123456", 3306).AsString);
mySQLConn.Open();
#region-----------------查询,利用MySQLDataAdapter查询----------------------
Console.WriteLine("-----------------查询 操作--------------------------------");
string querySQL = "select * from testdata LIMIT 100";
MySQLDataAdapter mySQLAdapter = new MySQLDataAdapter(querySQL,mySQLConn);
DataTable mySQLDataTable = new DataTable();
mySQLAdapter.Fill(mySQLDataTable);
foreach (DataRow dr in mySQLDataTable.Rows)
{
for (int j = 0; j < dr.ItemArray.Length; j++)
{
string str = dr[j].ToString();
}
Console.WriteLine("数据"+dr["data"]);
}
Console.WriteLine("条数:" + mySQLDataTable.Rows.Count);
#endregion
#region---------------查询,利用 MySQLDataReader,依次读取每一条数据-------------------
MySQLCommand mySQLCommand = new MySQLCommand(querySQL,mySQLConn);
MySQLDataReader mySQLReader = (MySQLDataReader)mySQLCommand.ExecuteReader();
int i = 0;
while (mySQLReader.Read())
{
i++;
for (int j = 0; j < mySQLReader.FieldCount; j++)
{
string str = mySQLReader[j].ToString();
}
Console.WriteLine("数据:" + mySQLReader["data"].ToString());
}
mySQLReader.Close();
Console.WriteLine("-----------------操作结束--------------------------------");
#endregion
#region ---------------------------插入操作------------------------------------
Console.WriteLine("---------------------直接插入 操作--------------------------");
string x = "11101111";
int x1 = 101;
int x2 = 101;
int x3 = 101;
int x4 = 101;
string insertSQL = "insert into testdata(data,data1,data2,data3,data4) values(" + x + "," + x1 + "," + x2 + "," + x3 + "," + x4 + ")";
MySQLCommand insertCommand = new MySQLCommand(insertSQL,mySQLConn);
insertCommand.ExecuteNonQuery();
Console.WriteLine("-----------------操作结束--------------------------------");
#endregion
Console.WriteLine("-----------------更新 操作--------------------------------");
string updateSQL = "UPDATE TESTDATA SET DATA=11011,DATA1=11011,DATA2=11011,DATA3=11011,DATA4=11011 WHERE data = 1011";
MySQLCommand updateCmd = new MySQLCommand(updateSQL, mySQLConn);
updateCmd.ExecuteNonQuery();
Console.WriteLine("-----------------操作结束--------------------------------");
mySQLConn.Close();
}
#endregion