Search code examples
c#sqloledb

get the protein that contains specific sequence using OLEDB


Here is a background on my program: each protein is made from a sequence of amino acids(or AA)

I have some tables :tblProInfo(that contains general info about proteins),tblOrderAA(that contains the sequence(AA sequence) of specific protein(for each protein there is a serial number that i set before))

now, I'm trying to retvive the science names of the protein that contains part of sequence that the user put in textbox1. It is likely that more than one protein contains the sequence that the user typed.

Here is my code. I got "Syntax error" and I'm sure I have more mistakes.Please HELP me!

        public void OpenDB()
    {
        dataConnection = new OleDbConnection();
        try
        {
            dataConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
            dataConnection.Open();
        }
        catch (Exception e)
        {
            MessageBox.Show("Error accessing the database: " +
                             e.Message,
                             "Errors",
                             MessageBoxButtons.OK,
                             MessageBoxIcon.Error);
        }
    }

private string FromCodonsToProtein(string codons)
    {
        OpenDB();
        int sizePro=0, i,counter=0,serialPro;
        string st="",tempst="";

        OleDbCommand datacommand = new OleDbCommand();
        datacommand.Connection = dataConnection;
        datacommand.CommandText = "SELECT tblProInfo.proInfoAAnum, tblProInfo.proInfoSerialNum,tblProInfo.proInfoScienceName FROM tblProInfo";
        OleDbDataReader dataReader = datacommand.ExecuteReader();
        while(dataReader.Read())
        {
            sizePro = dataReader.GetInt32(counter);
            serialPro= dataReader.GetInt32(counter+1);
            counter++;
              OleDbCommand cmd= new OleDbCommand();
              cmd.Connection = dataConnection;
              cmd.CommandText = "SELECT tblOrderAA.orderAACodon1 FROM tblOrderAA"
                               +"WHERE (((tblOrderAA.orderAASerialPro)='"+serialPro+"'))";

              OleDbDataReader rdr = cmd.ExecuteReader();
            tempst="";
            for (i = 0; i > sizePro; i++)
            {
                tempst = tempst + rdr.GetString(i);
            }
            if (tempst.Contains(codons))
            {
                st = st + " \n" + dataReader.GetString(counter);
            }
        }
            return st;


    }

Solution

  • Missing a space here

        cmd.CommandText = "SELECT tblOrderAA.orderAACodon1 FROM tblOrderAA" 
                           +"WHERE (((tblOrderAA.orderAASerialPro)='"+serialPro+"'))"; 
    

    rewrite in this way

        cmd.CommandText = "SELECT tblOrderAA.orderAACodon1 FROM tblOrderAA"  
                           +" WHERE (((tblOrderAA.orderAASerialPro)='"+serialPro+"'))"; 
                          // ^ here 
    

    However you should use parametrized query (also with msaccess) to avoid possible errors and injection attacks.
    Another problem is the global dataConnection. Don't do that, you gain nothing in this way.
    Return the connection and encapsulate it with a using statement.

    For example:

    public OleDbConnection OpenDB()   
    {   
        dataConnection = new OleDbConnection();   
        dataConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";   
        dataConnection.Open();   
        return dataConnection;
    }
    

    then in the calling code use this syntax

    using(OleDbConnection cnn = OpenDB())
    {
        // in the rest of your code, replace dataConnection with cnn
        // The using statement will ensure that in the case of exceptions
        // your connection will be allways closed and properly disposed
    
        ........
    
    }
    

    EDIT: Can't give you a full working solutions, too many aspects of your problem are unknown to me, however a great simplification will be to change your query in this way

    SELECT DISTINCT 
           tblProInfo.proInfoAAnum, 
           tblProInfo.proInfoSerialNum,
           tblProInfo.proInfoScienceName 
    FROM   tblProInfo LEFT JOIN tblOrderAA 
      ON   tblOrderAA.orderAASerialPro = tblProInfo.proInfoSerialNum
    
    WHERE  tblOrderAA.orderAACodon1 = @codons
    

    Try it directly in access using its query editor, if it works as you expected then change your code. You don't need two query and crossed loops to get the results.