Home > Mobile >  Load data from database to string
Load data from database to string

Time:04-19

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:

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?

CodePudding user response:

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;
    }
}
  • Related