前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#连接数据库插入数据

C#连接数据库插入数据

作者头像
杨奉武
发布2018-12-14 16:28:03
2.6K0
发布2018-12-14 16:28:03
举报
文章被收录于专栏:知识分享知识分享

首先是安装JDBC操作数据库的包,,当然自己看着办哈,可以自己下载以后导入,或者直接让软件本身下载

 第一种方式

 第二种 咱自己下载个低版本的

点击这个链接

点击以后呢可以直接下载下来,然后导入(大家百度怎么导入.dll哈)

也可以直接让软件自己弄

 安装好了

代码语言:javascript
复制
namespace MqttDemo
{
    class DataBase
    {
        public static MySqlConnection getMySqlCon(string username, string password, string ip, string port, string iotdatebase, string charset)
        {
            String connetStr =
                "server= " + ip +
                ";port=" + port +
                ";user =" + username +
                ";password=" + password +
                ";database =" + iotdatebase +
                ";charset =" + charset;
            // String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
            MySqlConnection mysql = new MySqlConnection(connetStr);
            return mysql;
        }

        public static void getInsert(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                String message = ex.Message;
                Console.WriteLine("插入数据失败了!" + message);
            }

        }

        //插入SQL组合
        public static String groupInsert(String from, params String[] arg)
        {
            String Value = arg[0];
            bool bl = false;
            foreach (String str in arg)
            {
                if (bl == true) Value = Value + "," + str;
                else bl = true;
            }
            String sql = "insert into " + from + " values(NULL," + Value + ")";
            return sql;
        }
        public static String groupInsert(String from, String[] option, String[] value)
        {
            String Option = option[0];
            String Value = "'" + value[0];
            bool bl = false;
            foreach (String str in option)
            {
                if (bl == true) Option = Option + "," + str;
                else bl = true;
            }
            bl = false;
            foreach (String str in value)
            {
                if (bl == true) Value = Value + "'" + "," + "'" + str;
                else bl = true;
            }
            Value = Value + "'";
            String sql = "insert into " + from + "(" + Option + ")" + " values(" + Value + ")";
            return sql;
        }
    }
}

点击按钮连接数据库

代码语言:javascript
复制
        private void button18_Click(object sender, EventArgs e)
        {
            string name = textBox21.Text.ToString();
            string password = textBox22.Text.ToString();
            string ip = textBox23.Text.ToString();
            string port = textBox24.Text.ToString();
            string database = textBox26.Text.ToString();
            string charset = textBox27.Text.ToString();
            if (name.Length > 0)
            {
                if (password.Length > 0)
                {
                    if (ip.Length > 0)
                    {
                        if (port.Length > 0)
                        {
                            if (database.Length > 0)
                            {
                                if (charset.Length > 0)
                                {
                                    if (button18.Text == "连接")
                                    {
                                        mySqlConnection = DataBase.getMySqlCon(name, password, ip, port, database, charset);
                                        mySqlConnection.Open();

                                        //MessageBox.Show("连接了", "234324");
                                        button18.Text = "断开";
                                    }
                                    else
                                    {
                                        button18.Text = "连接";
                                        try
                                        {
                                            mySqlConnection.Close();
                                        }
                                        catch (Exception)
                                        {

                                            throw;
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("编码方式不能为空", "提示!");
                                }
                            }
                            else
                            {
                                MessageBox.Show("数据库名字不能为空", "提示!");
                            }
                        }
                        else
                        {
                            MessageBox.Show("端口号不能为空", "提示!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("密码不能为空", "提示!");
                    }
                }
                else
                {
                    MessageBox.Show("密码不能为空", "提示!");
                }
            }
            else
            {
                MessageBox.Show("用户名不能为空", "提示!");
            }
        }

 。。。不能写了,忘了一件事情。。需要先安装数据库

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档