w605283073的专栏

先下载和安装MySQLDriverCS

(增)插入数据:using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();//sql语句string sql = "insert into tbl_sysuser (usercode,username,password,usertype,isActived) values(\&;fc02\&;,\&;蔡倩倩\&;,\&;w1251314\&;,\&;房产局管理员\&;,\&;YES\&;)";//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();//执行sql语句MySQLCommand cmd = new MySQLCommand(sql, conn);//返回受影响行数int number = cmd.ExecuteNonQuery();//关闭数据库conn.Close();Console.WriteLine("受影响的行数:" + number);}

(删)删除数据

using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();////sql语句string sql = "delete from tbl_sysuser where usercode=\&;fc02\&;";//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();////执行sql语句MySQLCommand cmd = new MySQLCommand(sql, conn);////返回受影响行数int number = cmd.ExecuteNonQuery();Console.WriteLine("受影响的行数:"+number);//关闭数据库conn.Close();}(改)修改数据[注入值]

using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();//sql语句string sql = "update tbl_sysuser set isActived=@isActived where id=1";////执行sql语句MySQLCommand cmd = new MySQLCommand(sql, conn);//注入值cmd.Parameters.Add(new MySQLParameter("@isActived","YES"));////返回受影响行数int number = cmd.ExecuteNonQuery();Console.WriteLine("受影响的行数:" + number);//关闭数据库conn.Close();}(改)修改数据

using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();//sql语句string sql = "update tbl_sysuser set isActived=\&;YES\&; where id=1";//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();////执行sql语句MySQLCommand cmd = new MySQLCommand(sql, conn);////返回受影响行数int number = cmd.ExecuteNonQuery();Console.WriteLine("受影响的行数:"+number);//关闭数据库conn.Close();}(查)查询数据:

using (MySQLConnection conn = new MySQLConnection(newMySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();//sql语句string sql = "select * from tbl_sysuser";//通过DataAdapter适配器查询MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);//查询出的数据是存在DataTable中的,DataTable可以理解成为一个虚拟的表,DataTable中的一行为一条记录,一列为一个数据库字段DataTable dt = new DataTable();mda.Fill(dt);for (int i = 0; i < dt.Rows.Count;i++ ){Console.WriteLine("取出的行:"+ dt.Rows[i]["usercode"]+ "|" + dt.Rows[i]["username"]+ "|" + dt.Rows[i]["password"]+ "|" + dt.Rows[i]["usertype"]+ "|" + dt.Rows[i]["isActived"]);}//关闭数据库conn.Close();}(查)查询数据2

using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "housing", "root", "root").AsString)){conn.Open();//防止乱码MySQLCommand commn = new MySQLCommand("set names gb2312", conn);commn.ExecuteNonQuery();//sql语句string sql = "select * from tbl_sysuser";MySQLCommand cmd = new MySQLCommand(sql, conn);MySQLDataReader reader = cmd.ExecuteReaderEx();while (reader.Read()){if (reader.HasRows){Console.WriteLine("usercode:"+reader.GetString(1)+"–username:"+reader.GetString(2));}}//关闭数据库conn.Close();}

,听他第二十八次提起童年往事,每年的同一天和他庆祝生日,

w605283073的专栏

相关文章:

你感兴趣的文章:

标签云: