将参数添加到数据网格视图
Add Parameter to Data Grid View
我需要为搜索按钮添加一个参数来填充我的数据网格视图,但我不知道该怎么做。
try
{
conexaoBD();
strSQL = "SELECT " +
"a.nm_id 'ID', " +
"b.ch_nome 'Empresa', " +
"c.ch_nome 'Banco', " +
"a.ch_resultado 'Resultado' " +
"FROM tb_homologação_bancária_santander a INNER JOIN tb_parâmetros_empresas b ON a.nm_id_empresa = b.nm_id " +
"INNER JOIN tb_parâmetros_bancos c ON a.nm_id_banco = c.nm_id " +
"WHERE a.nm_id_empresa = @resultado_id_empresa";
DataSet ds = new DataSet();
da = new SqlDataAdapter(strSQL, conexao);
conexao.Open();
da.Fill(ds);
dgvResultado.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conexao.Close();
conexao = null;
comando = null;
}
我尝试创建一个 SqlCommand,但没有机会使用 DataSet。
有人可以帮我正确地插入@resultado_id_empresa作为参数吗?
谢谢!!
那行吗?
strSQL = "... WHERE a.nm_id_empresa = ?";
da = new SqlDataAdapter(strSQL, conexao);
da.SelectCommand.Parameters.Add("@ID", SqlDbType.Int /* or any relevant type*/).Value = ...;
我需要为搜索按钮添加一个参数来填充我的数据网格视图,但我不知道该怎么做。
try
{
conexaoBD();
strSQL = "SELECT " +
"a.nm_id 'ID', " +
"b.ch_nome 'Empresa', " +
"c.ch_nome 'Banco', " +
"a.ch_resultado 'Resultado' " +
"FROM tb_homologação_bancária_santander a INNER JOIN tb_parâmetros_empresas b ON a.nm_id_empresa = b.nm_id " +
"INNER JOIN tb_parâmetros_bancos c ON a.nm_id_banco = c.nm_id " +
"WHERE a.nm_id_empresa = @resultado_id_empresa";
DataSet ds = new DataSet();
da = new SqlDataAdapter(strSQL, conexao);
conexao.Open();
da.Fill(ds);
dgvResultado.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conexao.Close();
conexao = null;
comando = null;
}
我尝试创建一个 SqlCommand,但没有机会使用 DataSet。
有人可以帮我正确地插入@resultado_id_empresa作为参数吗?
谢谢!!
那行吗?
strSQL = "... WHERE a.nm_id_empresa = ?";
da = new SqlDataAdapter(strSQL, conexao);
da.SelectCommand.Parameters.Add("@ID", SqlDbType.Int /* or any relevant type*/).Value = ...;