Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Load data from database to string

I’m trying at the moment to get an information from a database and want to save it in a string, but yeah not sure about how really to do it right.

This is my code, where I open the LoadSql function:

public void LoadData(string KNR, string WNR, String filter)
{
    // WHERE
    const string sqlTemplate = "SELECT KUNDENAUFTRAGSNR FROM MESSFELD.AUSSTANDSDATEN WHERE FTNR = '" + txt_Barcode_read.Text + "'";
    string sql = string.Format(sqlTemplate, filter);

    Database cdb = new Database();

    // try to connect and cancel on error
    if (!cdb.Open("**********", "*********"))
    {
        SetStatusText("Datenbank ist nicht verfügbar.");
        return;
    }

    WNR = cdb.LoadSql(sql);
    cdb.Close();
}

And here is the LoadSQL function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public DataTable LoadSql(string sql)
{
    try
    {
        OracleCommand command = new OracleCommand(sql, _con);
        command.InitialLONGFetchSize = -1;

        OracleDataReader rdr = command.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(rdr);

        return dt;
    }
    catch
    {
        return null;
    }
}

For the moment the LoadSQL is saving in datatable, how to change for saving the number in the string WNR?

>Solution :

You can re-write your LoadSQL function to something like this :

public string LoadSql(string sql)
{
    try
    {
        OracleCommand command = new OracleCommand(sql, _con);
        command.InitialLONGFetchSize = -1;

        OracleDataReader rdr = command.ExecuteReader();
        rdr.Read();
        
        if(rdr.HasRows)
            return rdr.GetString(0);
        else
            return "";
    }
    catch
    {
        return null;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading