我试图从Microsoft Acess获取一些数据并通过MessageBoxes检查它,这样用户就可以看到这是否是他想要的数据,但是代码根本没有执行。我看不到任何消息框,也没有抛出任何异常。 下面是我放在Button事件中的代码。 (我只做了第一次,如果CNPJ那个)。
if (TipoConsulta.Text == "CNPJ")
{
try
{
string comando = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\\\10.7.41.153\\Apoio\\Davi_Database\\Base_Imo.accdb";
OleDbConnection Connection = new OleDbConnection(conn);
OleDbCommand Command = new OleDbCommand(comando, Connection);
Connection.Open();
OleDbDataReader reader = Command.ExecuteReader();
while (reader.Read())
{
foreach (OleDbDataReader resultado in reader)
{
DialogResult resultadoDialogo;
resultadoDialogo = MessageBox.Show("Encontrada razão social '" + resultado.ToString() + "'.\n Está correto?", "Pesquisa", MessageBoxButtons.YesNo);
if (resultadoDialogo == System.Windows.Forms.DialogResult.Yes)
{
MessageBox.Show("Executado CarregaImobiliaria().");
break;
}
else if (resultadoDialogo == System.Windows.Forms.DialogResult.No)
{
continue;
}
}
}
}
catch (InvalidOperationException)
{
MessageBox.Show("Nada foi encontrado com o CNPJ " + ValorConsulta.Text + ".", "Pesquisa");
}
}
else if (TipoConsulta.Text == "Razão Social")
{
MessageBox.Show("Tipo: Razão Social");
try
{
}
catch (InvalidOperationException)
{
}
}
else if (TipoConsulta.Text == "RGI")
{
MessageBox.Show("Tipo: Sub: RGI");
try
{
}
catch (InvalidOperationException)
{
}
}
答案 0 :(得分:0)
问题可能是缺少一个接收DataReader值的矩阵,因为foreach块从未被执行过。我最后使用了一段时间。
string _cmd = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\\\10.7.41.153\\Apoio\\Davi_Database\\Base_Imo.accdb";
OleDbConnection Connection = new OleDbConnection(conn);
OleDbCommand Command = new OleDbCommand(_cmd, Connection);
Connection.Open();
OleDbDataReader reader = Command.ExecuteReader();
while (reader.Read())
{
int _resMatriz = 0;
DialogResult result;
result = MessageBox.Show("Encontrado imobiliária: " + reader[_resMatriz] + ".\nEstá correto?", "Pesquisa", MessageBoxButtons.YesNo);
_resMatriz++;
if (result == System.Windows.Forms.DialogResult.Yes)
{
MessageBox.Show("CarregaImo()");
break;
}
else
{
continue;
}
}