需要mysql.data.dll
数据库连接
//server数据库地址,user数据库用户名,password密码,database数据库名
string ConnectString = string.Format("Server={0};User ID={1};Password={2};Database={3};", server, user, password, database);
MySqlConnection conn = new MySqlConnection(ConnectString );
conn.Open();
//加数据库操作
conn.Close();
conn.Dispose();
添加
MySqlCommand command = conn.CreateCommand();
string str = "INSERT INTO {0}(id,name)VALUES('{1}','{2}')";
//{0}表名,{1}字段id的值,{2}字段name的值
command.CommandText = string.Format(str, table, ID, Name);
command.ExecuteNonQuery();
删除
MySqlCommand command = conn.CreateCommand();
string str = string.Format("DELETE FROM {0} WHERE id='{1}'", table, IdNumber);
command.CommandText = str;
command.ExecuteNonQuery();
修改
MySqlCommand command = conn.CreateCommand();
string str = "UPDATE {0} SET id='{1}',name='{2}'";
command.CommandText = string.Format(str, table, Id, Name);
command.ExecuteNonQuery();
清空表
MySqlCommand command = conn.CreateCommand();
string str = string.Format("truncate table {0}", table);
command.CommandText = str;
command.ExecuteNonQuery();
查找
string command = string.Format("SELECT * FROM {0}", table);//
MySqlCommand mycmd = new MySqlCommand(command, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(mycmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string id = ds.Tables[0].Rows[i][0].ToString();
string name = ds.Tables[0].Rows[i][1].ToString();
}