C# 连接MYSQ示例(全面)

先下载和安装MySQLDriverCS 
http://sourceforge.net/projects/mysqldrivercs/

在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中

(增)插入数据:
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(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";

                //通过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();
            }



C# 连接MYSQ示例(全面)

上一篇:C#(Visual Studio) AssemblyInfo


下一篇:在emwin中显示字库芯片GT23L24M0140的字模