I
I
Ivan Vyrov2016-03-11 12:31:55
C++ / C#
Ivan Vyrov, 2016-03-11 12:31:55

How to automatically select the data type?

Good afternoon!
There is some function:

public List<List<string>> DateToLis(string date)
        {
            DataTable dt = Get("SELECT service.FullName,asuiogv.fl_req_peper + asuiogv.fl_req_other,asuiogv.fl_req_epgu,asuiogv.fl_req_email,asuiogv.fl_req_ved,asuiogv.fl_z_peper + asuiogv.fl_z_other,asuiogv.fl_z_epgu,asuiogv.fl_z_email,asuiogv.fl_z_ved,asuiogv.ul_req_peper + asuiogv.ul_req_other,asuiogv.ul_req_epgu,asuiogv.ul_req_email,asuiogv.ul_req_ved,asuiogv.ul_z_peper + asuiogv.ul_z_other,asuiogv.ul_z_epgu,asuiogv.ul_z_email,asuiogv.ul_z_ved FROM asuiogv INNER JOIN service ON service.id = asuiogv.id_service where period='" + date + "';");
            
            List<List<string>> data = new List<List<string>>(); 
            for (int i = 0; i < dt.Rows.Count-1; i++)
            {
                List<string> list = new List<string>();
                for (int j = 0; j < dt.Columns.Count-1; j++)
                {
                    list.Add(dt.Rows[i].Field<string>(dt.Columns[j].Caption));//здесь необходимо за место string автоматически подбирать тип данных в зависимости от БД
                }
                data.Add(list);
            }
            return data;
        }

it is necessary to do so that in the line
list.Add(dt.Rows[i].Field<string>(dt.Columns[j].Caption));
string changed depending on the table data, i.e. int64, int or string.
thought aside
TypeCode tc=Type.GetTypeCode(dt.Columns[dt.Columns[j].Caption)].DataType);
but when substituting instead of string, it gives an error.
What solution can be found in this case?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Kovalsky, 2016-03-11
@dmitryKovalskiy

Of course, I would like to say a little foul language, but I will probably gather my strength. How many fields do you have in the output? ten? If there were 500 of them, then your problem would be clear, but everything is solved easier. You must have an entity class whose object is created based on the received data.
Gather your strength and write prop1 = col1, prop2 = col2, etc. Each individual field has its own type, and if you don't have surprises there, everything will converge fine.
List<List<string>>this is not a type - this is crap. No entity data.

S
Stanislav Makarov, 2016-03-11
@Nipheris

Take advantage of C#'s "everything is an object":
1) replace List<List<string>>with List<List<object>>;
2) dt.Rows[i].Field<string>(dt.Columns[j].Caption)replace with dt.Rows[i][dt.Columns[j]];
3) explain the purpose of this entire code, because it resembles a transfusion from empty to empty, and List<List<object>>this is a bomb by definition.

V
V Sh., 2016-03-11
@JuniorNoobie

Use LINQ and be happy.

M
Michael, 2016-03-11
@Sing303

Dmitry Kovalsky says that at least you should have an entity for each table and there should also be an entity for each query that you execute. Look at how the work with NHibernate and EF is going on and there will be no such questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question