简体   繁体   中英

Too much connections to db using DataReader

Every time i get a value from a response from a odbcconnection by datareader, i made a connection to the database (if i have a query that return 9 fields, i have 9 connection to db), and i want to do only 1 connection and retrieve all information. it's possible with datareader ? I need to use other method of connection ?

Best Regards.

Code:

    string strSql = "SELECT G.COMPANY_ID, U.USER_ID, U.GROUP_ID, U.NAME, U.DISPLAY_NAME, U.EMAIL, U.IS_CORPORATE, U.CALL_PARK, U.CALL_PICKUP, U.PCHUNTING, U.OUT_OF_OFFICE, U.DND, U.HOTLINE, U.PIN, U.FORCE_PIN_CHECKED, U.PCHUNTING_TYPE, U.DND_END_TIMESTAMP, U.DND_CONTACT, U.OUT_OF_OFFICE_TYPE, U.LANGUAGE, U.AVAILABLE_TIMESTAMP, U.LAST_DIALLED_NUMBER, U.LAST_INCOMING_CALL, U.LAST_MISSED_CALL, U.CALL_PICKUP_GROUP_ID, U.HOTLINE_NUMBER, U.PORTAL_PASSWORD, U.PROFILE, U.MAIN_NUMBER, U.DUAL_OUTGOING_CTRANSFER, U.MY_CALL_PICKUP, U.VM_RECONNECT_NOTIFY, U.SPARE_STRING1, U.INSERT_DATE, U.INSERT_USER, U.UPDATE_DATE, U.UPDATE_USER " +
                            "FROM {0}_TT_USER U LEFT OUTER JOIN {0}_TT_GROUP G ON  U.GROUP_ID = G.GROUP_ID " +
                            "WHERE USER_ID = :USER_ID ";

            conn = new OdbcConnection(GetIpCntrxTimestenConnString(opCode));
            cmd = new OdbcCommand(
                 string.Format(strSql
                             , config.GetIpCntrxEsmViewName(opCode))
                 , conn);

            cmd.Parameters.AddWithValue(":USER_ID", user_id);

            cmd.CommandType = CommandType.Text;
            conn.Open();

            dataReader = cmd.ExecuteReader();

            object[] meta = new object[dataReader.FieldCount];

            int NumberOfColums = dataReader.GetValues(meta);

No, you need another way to query.

Instead of one query per field, SELECT all 9 at once and close the connection right away.

Another problem with this approach is that there's no layering whatsoever. I wouldn't like mingling UI and database code together. There's no abstraction your way.

Try using a StringBuilder and AppendLine your select queries. Loop through your dbReader and store in an List thats the best i can come up with. I do understand you want one call with 9 different queries but either way there is going to be overhead on your sql machine or program machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM