MySql.Data.dll

时间:2015-06-03 08:54:46

标签: c# mysql

                           //codigo de pesquisa
        string strconexao = "persist security info =false ; server=localhost ; database= iconefileequipamentos; uid=root";
        MySqlConnection oConn =new MySqlConnection(strconexao);
        StringBuilder strSQL =new StringBuilder();
        DataTable oTable =new DataTable();
        MySqlCommand oCmd =new MySqlCommand();

        oConn.Open();

        if (comboBox1.Text =="NomeResponsavel")
        {
            string NomeResponsavel = textBox1.Text; 
            strSQL.Append("Select *");
            strSQL.Append("FROM responsavelpc");
            strSQL.Append("Where Nomeresponsavel like '" + NomeResponsavel + "'");
        }

        if (comboBox1.Text == "NomeEquipamento")
        {
            string NomeEquipamento = textBox1.Text;
            strSQL.Append("Select *");
            strSQL.Append("FROM responsavelpc");
            strSQL.Append("Where NomeEquipamento like'" + NomeEquipamento +              "'");
        }
        MySqlDataAdapter oDA = new MySqlDataAdapter(strSQL.ToString(), oConn);
        oDA.Fill(oTable); hear appears the message that i put as title!
        dataGridView1.DataSource = oTable;
        oConn.Close();           
    }

这个代码是为了一个shearch我的表单有一个dataGridView1一个comboBox1和一个按钮一个textBox1使用我们必须选择我们有的示例nomeequipamento然后我们在textBox中放入equipamento的名称。如果代码是垃圾,但我是一个nobbie:)

1 个答案:

答案 0 :(得分:1)

我认为这可能是因为错过了空白区域。但这只是我疯狂的猜测。

这是更正后的代码。我添加了评论行

//codigo de pesquisa
        string strconexao = "persist security info =false ; server=localhost ; database= iconefileequipamentos; uid=root";
        MySqlConnection oConn =new MySqlConnection(strconexao);
        StringBuilder strSQL =new StringBuilder();
        DataTable oTable =new DataTable();
        MySqlCommand oCmd =new MySqlCommand();

        oConn.Open();

        if (comboBox1.Text =="NomeResponsavel")
        {
            string NomeResponsavel = textBox1.Text; 
            //Added white space at end of each line
            strSQL.Append("Select * ");
            strSQL.Append("FROM responsavelpc ");
            strSQL.Append("Where Nomeresponsavel like '" + NomeResponsavel + "'");
        }

        if (comboBox1.Text == "NomeEquipamento")
        {
            string NomeEquipamento = textBox1.Text;
            //Added white space at end of each line
            strSQL.Append("Select * ");
            strSQL.Append("FROM responsavelpc ");
            //Added white space after LIKE
            strSQL.Append("Where NomeEquipamento like '" + NomeEquipamento +"'"); 
        }
        MySqlDataAdapter oDA = new MySqlDataAdapter(strSQL.ToString(), oConn);
        oDA.Fill(oTable); hear appears the message that i put as title!
        dataGridView1.DataSource = oTable;
        oConn.Close();           
    }

PS:如果你想实现搜索功能,你必须使用LIKE'%search_term%'。您缺少代码中的%符号。除此之外,您的代码根本不安全。改为使用参数化查询。