VS2017中运行MySQL的存储过程

时间:2022-07-23
本文章向大家介绍VS2017中运行MySQL的存储过程,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        MySqlConnection conn;
        //string str = "server=DESKTOP-F3U79IB\MySQL;database=test;uid=root;pwd=a123456.";
        string str = "server=127.0.0.1;database=test;uid=root;pwd=a123456.";
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new MySqlConnection(str);
            conn.Open();
            if (conn.State == ConnectionState.Open)
               MessageBox.Show( "数据库连接n状态:成功");
            else
                MessageBox.Show("数据库连接n状态:失败");

             /*执行mysql命令*/
            string sql1 = "insert into stu(Name,Sex,Age) values('zyr7','man', 28)";
            MySqlCommand cmd = new MySqlCommand(sql1,conn);
            cmd.ExecuteNonQuery();


            /*执行存储过程*/
            MySqlCommand  cmd1 = new MySqlCommand();
            cmd1.Connection = conn;
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.CommandText = "new_procedure";//存储过程名..
            cmd1.ExecuteNonQuery();

        }
    }
}

mysql: