class Program
{
static void Main(string[] args)
{
//数据库的增删改查
string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);//连接数据库;
mySqlcon.Open();
//查
string str = "select * from student where sid=3;";
MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
CheckedMySql(mySqlCom);
//增
str = "insert into student values(15,'神10',28,'女');";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
//删
str = "delete from student where sid=5;";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
// 改
str = "update student set sage=33 where sid=9;";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
GetId(9);
Console.ReadKey();
}
//(查)
static private void CheckedMySql(MySqlCommand mySqlcom)
{
MySqlDataReader read = mySqlcom.ExecuteReader();
try
{
while (read.Read())
{
if (read.HasRows)//查找
{
Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", read.GetInt32(0), read.GetString(1), read.GetInt32(2), read.GetString(3));
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
read.Close();
}
}
//增 删 改 查
static private void IducMySql(MySqlCommand mySqlcom)
{
try
{
mySqlcom.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
//指定ID参数打印相关信息
static private void GetId(int id)
{
string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);
mySqlcon.Open();
string str = "select * from student where sid=" + id;//注意 别把分号打上去;
MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
MySqlDataReader reader = mySqlCom.ExecuteReader();
try
{
while (reader.Read())
{
if (reader.HasRows)//查找
{
Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2), reader.GetString(3));
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
reader.Close();
}
}
}
网友评论