c# - 存储过程的执行未运行

标签 c# mysql stored-procedures

我在调用存储过程过滤客户数据时遇到问题。 表:

ID   : CS-001, CS-002, CS-003  
Nama : xxxxxx, xxxxxx, xxxxxx            

C# 中的函数

public DataTable pilihCustomer(string id)
{
    classKoneksi = new koneksi();
    sql = "call PilihCustomer(" + id + ")";
    tabel = new DataTable();

    try
    {
        classKoneksi.koneksiBuka();
        komand = new MySqlCommand(sql, classKoneksi.konek);
        adapter = new MySqlDataAdapter(komand);
        adapter.Fill(tabel);
    }
    catch (Exception) {}

    classKoneksi.koneksiTutup();
    return tabel;
}

我称之为

private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (LvCustomer.SelectedItems.Count > 0)
    {            
        string id = LvCustomer.SelectedItems[0].Text;
        FCustomer f2 = new FCustomer();
        DATA_ACCES.Acces_Customer baca = new DATA_ACCES.Acces_Customer();
        tabel = baca.pilihCustomer(id);
        string  nama = "", npwp = "", tlp = "", fax = "", email="", kontak="";
        foreach (DataRow kolom in tabel.Rows)
        {
            nama = kolom["nama perusahaan"].ToString();
            npwp = kolom["npwp"].ToString();
            tlp = kolom["telepon"].ToString();
            fax = kolom["faxmile"].ToString();
            email = kolom["email"].ToString();
            kontak = kolom["kontak"].ToString();
        }
    }
    else
    {
        MessageBox.Show("Please select an item before assigning a value.");
    }
}

如果表格为

,则可以运行上面的函数
ID   : 101, 102, 103  
Nama : xxxxxx, xxxxxx, xxxxxx            

最佳答案

id 参数是一个字符串,因此需要用单引号括起来,因此将生成 SQL 的行更改为:

sql = "call PilihCustomer('" + id + "')";

但是,正如其他评论者提到的那样,您的代码非常不安全,您应该使用参数化查询。

关于c# - 存储过程的执行未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399495/

相关文章:

c# - 表单之间通信的最佳方式?

c# - Paypal 错误从 Asp.net 中的传输流接收到意外的 EOF 或 0 字节

php - 在 WordPress 中处理表单的 PHP 文件应该保存在哪里?

oracle - 如何将 Oracle 过程的参数默认值设置为选择结果?

C# 命名空间 : Calling Methods and Classes

MySQL LEFT JOIN 在第二个表中具有可选值

Mysql 使用带有月份的月份名称

database - 如何通过数据库链接执行 Oracle 存储过程

MySQL - 存储过程

c# - 需要viber webservice或api地址