C#使用预处理语句

时间:2016-10-05 03:56:25

标签: c# mysql .net

我想使用C#

将mysql数据填充到gridview中

查询正在使用NodeJS + mysql,但在使用C#时会产生错误。

节点JS代码:

app.get('/allhistorytalenta',function(req,res){
    client.query("SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(mulai = ''',mulai,''', talenta, NULL)) AS ', QUOTE(mulai)) ORDER BY MULAI ASC ) INTO @sql FROM history_talenta; SET @sql = CONCAT('SELECT nip, nama, DATE_FORMAT(tgl_grade_terakhir,''%d-%m-%Y'') as tgl_grade_terakhir, ', @sql, ' FROM history_talenta GROUP BY nip'); PREPARE stmt FROM @sql; EXECUTE stmt;", function(err,rows_final){                                                
    res.render('pages/alltalenta',{ data2 : rows_final, data : req.session.user, datarole : req.session.role, moment : moment } );
        console.log("Nilai Row Final : "+rows_final[3][1].nama);
    });
});

这是我在C#中的代码:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;

namespace DataGridView_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        private void BindGrid()
        {
            string conString = @"Data Source=10.16.107.77;port=3306;Initial Catalog=sdmbook;User Id=root;password=xxxx";
            using (MySqlConnection con = new MySqlConnection(conString))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(mulai = ''',mulai,''', talenta, NULL)) AS ', QUOTE(mulai)) ORDER BY MULAI ASC ) INTO @sql FROM history_talenta; SET @sql = CONCAT('SELECT nip, nama, DATE_FORMAT(tgl_grade_terakhir,''%d-%m-%Y'') as tgl_grade_terakhir, ', @sql, ' FROM history_talenta GROUP BY nip'); PREPARE stmt FROM @sql; EXECUTE stmt;", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            dataGridView1.DataSource = dt;
                        }
                    }
                }
            }
        }
    }
}

查询实际上是从数据转换:

enter image description here

进入:

enter image description here

如何使用C#和mysql以正确的方式执行我的查询?

谢谢

0 个答案:

没有答案