当查询时间过长时,经常会遇到这种情况。我已经定义了参数@no_registrasi,但是它仍然出错。
MySql.Data.MySqlClient.MySqlException:命令执行期间遇到致命错误。 ---> MySql.Data.MySqlClient.MySqlException:必须定义参数'@no_registrasi'。
这是我的代码:
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();
}
答案 0 :(得分:1)
因为您将参数添加到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];
}