参数@

643ylb08  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(259)

当我的查询太长时,我经常遇到这种情况。我已经定义了参数@no\u registrasi,但它仍然是错误的。
mysql.data.mysqlclient.mysqlexception:执行命令时遇到致命错误。-->mysql.data.mysqlclient.mysqlexception:必须定义参数'@no\u registersi'。
这是我的密码:

void Tombol_cari_simpanan1Click(object sender, EventArgs e)
    {
        string connectionString = "Server=localhost;User ID=root;Password=;Database=koperasi;Convert Zero Datetime=True;";
        MySql.Data.MySqlClient.MySqlConnection connect = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
        connect.Open();
        string query = "select * from simpanan left join anggota on simpanan.no_registrasi = anggota.no_registrasi left join jenis_simpanan on simpanan.id_jenis_simpanan = jenis_simpanan.id_jenis_simpanan where simpanan.no_registrasi = @no_registrasi";
        MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(query, connect);
        myCommand.Parameters.AddWithValue("@no_registrasi", int.Parse(textbox_pencarian_no_registrasi.Text));
        MySql.Data.MySqlClient.MySqlDataReader reader=myCommand.ExecuteReader();
        connect.Close();
        mySqlDataAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter(query, connect);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        datagrid_simpanan.DataSource = DS.Tables[0];
        connect.Close();    
    }
3vpjnl9f

3vpjnl9f1#

因为你在 MySqlCommand myCommand 对象。
但你也可以通过 mySqlDataAdapter ,该对象未设置任何参数。
从你的代码中关于 MySqlCommand myCommand 因为你没有使用

MySql.Data.MySqlClient.MySqlDataReader reader=myCommand.ExecuteReader();

我想你可以试试这个。

void Tombol_cari_simpanan1Click(object sender, EventArgs e)
{
    string connectionString = "Server=localhost;User ID=root;Password=;Database=koperasi;Convert Zero Datetime=True;";
    string query = "select * from simpanan left join anggota on simpanan.no_registrasi = anggota.no_registrasi left join jenis_simpanan on simpanan.id_jenis_simpanan = jenis_simpanan.id_jenis_simpanan where simpanan.no_registrasi = @no_registrasi";

    MySql.Data.MySqlClient.MySqlConnection connect = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
    connect.Open();

    mySqlDataAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter(query, connect);
    DataSet DS = new DataSet();
    mySqlDataAdapter.SelectCommand.Parameters.AddWithValue("@no_registrasi", int.Parse(textbox_pencarian_no_registrasi.Text));
    mySqlDataAdapter.Fill(DS);
    datagrid_simpanan.DataSource = DS.Tables[0];
    connect.Close();
}

注:
我会用 using 要包含的关键字

MySqlConnection connect = new MySqlConnection(connectionString)

因为什么时候 connect 对象离开它将调用的范围 IDisposable.Dispose() 自动接口方法。

string connectionString = "Server=localhost;User ID=root;Password=;Database=koperasi;Convert Zero Datetime=True;";
string query = "select * from simpanan left join anggota on simpanan.no_registrasi = anggota.no_registrasi left join jenis_simpanan on simpanan.id_jenis_simpanan = jenis_simpanan.id_jenis_simpanan where simpanan.no_registrasi = @no_registrasi";
DataSet DS = new DataSet();
using (MySqlConnection connect = new MySqlConnection(connectionString))
{
    connect.Open();
    mySqlDataAdapter = new MySqlDataAdapter(query, connect);
    mySqlDataAdapter.SelectCommand.Parameters.AddWithValue("@no_registrasi", int.Parse(textbox_pencarian_no_registrasi.Text));
    mySqlDataAdapter.Fill(DS);
    datagrid_simpanan.DataSource = DS.Tables[0];
}

相关问题