mysql hollo world

using System;
using MySql.Data.MySqlClient;

namespace testdatabase
{
    internal class Program
    {
        public static void Main(string[] args)//192.168.1.103    127.0.0.1   192.168.1.101   211.161.243.46  103.44.145.245
        {
            var mysqlStr =
                //"Database=wordsgame;Data Source=211.161.243.46;User Id=admin;Password=wq123;pooling=false;CharSet=utf8;port=3306";

                "Database=wordsgame;Data Source=192.168.1.101;User Id=admin;Password=wq123;pooling=false;CharSet=utf8;port=3306";
            var mysql = new MySqlConnection(mysqlStr);
            //查询sql

            var abc = "person";
            var sqlSearch = "select * from ";
            sqlSearch = sqlSearch + abc;

//            Console.WriteLine("Input Name");
//            var readName = Console.ReadLine();
//            Console.WriteLine("Input ID");
//            var readID = Console.ReadLine();
//            Console.WriteLine("Input Motto");
//            var readMotto = Console.ReadLine();
//            Console.WriteLine("Input Tel");
//            var readTel = Console.ReadLine();
//
//            var sqlinsert = "insert into person values(" +
//                            "'" + readName + "'"
//                            + "," +
//                            "'" + readID + "'"
//                            + "," +
//
//                            "'" + readMotto + "'"
//                            + "," +
//                            "'" + readTel + "'"
//                            + ")";

            string sqlComm = "UPDATE person SET motto='telephone<10' WHERE telephone < 10 ";
            var mySqlCommand = new MySqlCommand(sqlComm, mysql);


            mysql.Open();

            var reader = mySqlCommand.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    if (reader.HasRows)
                    {
                        Console.WriteLine("name:" + reader.GetString(0) + "        |id:" + reader.GetInt32(1) +
                                          "        |motto:" + reader.GetString(2) + "  |tel:" + reader.GetString(3));
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("查询失败了!");
            }
            finally
            {
                reader.Close();
            }


            mysql.Close();
            Console.Read();
        }

        /// <summary>
        ///     建立mysql数据库链接
        /// </summary>
        /// <returns></returns>
        public static MySqlConnection getMySqlCon()
        {
            var mysqlStr =
                "Database=test;Data Source=127.0.0.1;User Id=root;Password=zycht520;pooling=false;CharSet=utf8;port=3306";
            // String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
            var mysql = new MySqlConnection(mysqlStr);
            return mysql;
        }

        /// <summary>
        ///     建立执行命令语句对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="mysql"></param>
        /// <returns></returns>
        public static MySqlCommand getSqlCommand(string sql, MySqlConnection mysql)
        {
            var mySqlCommand = new MySqlCommand(sql, mysql);

            //  MySqlCommand mySqlCommand = new MySqlCommand(sql);
            // mySqlCommand.Connection = mysql;
            return mySqlCommand;
        }

        /// <summary>
        ///     查询并获得结果集并遍历
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getResultset(MySqlCommand mySqlCommand)
        {
            var reader = mySqlCommand.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    if (reader.HasRows)
                    {
                        Console.WriteLine("编号:" + reader.GetInt32(0) + "|姓名:" + reader.GetString(1) + "|年龄:" +
                                          reader.GetString(2));
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("查询失败了!");
            }
            finally
            {
                reader.Close();
            }
        }

        /// <summary>
        ///     添加数据
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getInsert(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                Console.WriteLine("插入数据失败了!" + message);
            }
        }

        /// <summary>
        ///     修改数据
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getUpdate(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                Console.WriteLine("修改数据失败了!" + message);
            }
        }

        /// <summary>
        ///     删除数据
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getDel(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                Console.WriteLine("删除数据失败了!" + message);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值