使用SqlDataReader确定行的数据大小

时间:2015-05-26 03:30:25

标签: c# sql ado.net

我正在尝试确定从SQL Server返回到.NET应用程序的任意SQL命令的数据量。列结构在编译时是未知的。我对数据本身并不感兴趣,只是长度。

long size = 0;
using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        //size += size of current reader row ?
    }
}

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

如果行的大小表示列数,则可以使用reader。FieldCount或reader。VisibleFieldCount

<?php 

//Returns an associative array with the query result:
    function select($yourSQLQuery){
        //Array with result:
        $result = array();

        //Database conection
        $db = new PDO($dsn,$username,$password);

        $stmt = $db->query($yourSQLQuery);

        //This going to save an array with your data:
        $result = $stmt->fetchAll(PDO::FETCH_NUM);

        $db = null;

        return $result;
    }

    //*********************************************************************************************
    //Do here your query:
    $result = select("SELECT DISTINCT activity, logdate FROM NetLog ORDER BY activity");
    //*********************************************************************************************


    //Form handler: 
    if($_SERVER[REQUEST_METHOD] == "POST"){
        //If the form was submited:

        //Get   selected activity
        if ( isset($_POST['activities']) ) {

    /*Instead of sending your activity you can send the number of the submitted record in $records, then extract activity and logdate and make your query:*/
            $rowNumber = $_POST['activities'];

            //Get your log date:
            $act = $result[$rowNumber]; //if doesn't work try '$rowNumber'
            $activity = $act['activity'];
            $logdate = $act['logdate'];

            //Pull records asocciated with submitted activity:          
            $sql = "SELECT * FROM putHereYourTable WHERE activity = '$activity' AND logdate='$logdate'";
            $records = select($sql);

            //Pulled activities are now in $records 

            //do something with the records that you want. e.g.:
            print_r($records);


        }
    }   
?>

<!-- Your HTML: -->
<form action="define_activity" id="activityform" method="post">   <!-- Form to select an activity -->
        <select name="activities" id="select1">      <!-- List of activities -->
            <option value="" selected="">Select One</option>
            <option value="NewActivity" id="newactivity"       onclick="newactivity();">New Activity</option>
                <?php                   
                    foreach($result as $key => $act) {
                        //Send the number of register instead of $act[activity]:
                        echo ("<option value='$key'>$act[activity] of $act[logdate]</option>");
                    }
                ?>
            </select>
            <input type="submit" name = "Submit" />
</form>