为什么数据在插入db后会被截断?

时间:2014-01-27 21:21:25

标签: c# mysql

string myParameters = "serial=" + serial + "&command_id=" + cmd_id + "&successfullyExecuted=1&command_type=" + getCommandType(cmd) + "&answer=" + executeCommand(cmd);

using (WebClient wc22 = new WebClient())
{
wc22.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

string HtmlResult = wc22.UploadString(URI, myParameters);
}

getCommand(cmd)返回一个非常长的字符串(大约1.70 MB)。当我使用File.WriteAllText(“debug.txt”)将其写入文本文件时;我可以看到所有的数据。但是,在我的数据库中,我只看到了它的一部分。它被截断了。

我的sql列类型是“LONG TEXT”

编辑(结果是执行executeCommand()返回的内容):

StringWriter stringw = new StringWriter();
Console.SetOut(stringw);

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
DirSearch(d.Name, commandContent);  

}

result = stringw.ToString();
File.WriteAllText("debug.txt", result); // <-- This file contains full data 

数据库是MySQL,我正在使用phpMyAdmin直接查看数据

这是我要发布的页面:

 <?php 

if (isset($_POST['serial'])) {


//Include the database connection file
include "database_connection.php"; 
// prevent sql injection
$serial = mysql_real_escape_string(($_POST['serial']));

    if (isset($_POST['successfullyExecuted'],$_POST['command_id'],$_POST['command_type'],$_POST['answer'])) {
    $cmdID = mysql_real_escape_string($_POST['command_id']);
    $cmdType = mysql_real_escape_string($_POST['command_type']);
    $answer = mysql_real_escape_string($_POST['answer']);
    $executeSuccess = mysql_real_escape_string($_POST['successfullyExecuted']);
    if ("1" == $executeSuccess) {
       // TRUE : Command executed successfully 
       $q = "UPDATE `eu181976_kl`.`commands` SET `whether_executed` = 1,`answer_type` = '".$cmdType."',`answer` = '".$answer."' WHERE `commands`.`to_whom` = '".mysql_real_escape_string($serial)."' AND `commands`.`id` = '".mysql_real_escape_string($cmdID)."' ;";
       $updateNow = mysql_query($q);
    } else if ("0" == $executeSuccess) {
        // FALSE : There was an error executing the command
        $q = "UPDATE `eu181976_kl`.`commands` SET `whether_active` = 0,`answer_type` = '".$cmdType."' WHERE `commands`.`to_whom` = '".mysql_real_escape_string($serial)."' AND `commands`.`id` = '".mysql_real_escape_string($cmdID)."' ;";
        $updateNow = mysql_query($q);
    } else {
        // There was an error in the link
    echo "There was an error in query";

    }
    } else {
    //Check the database table for the logged in user information
    $q = "SELECT * FROM commands WHERE to_whom='".$serial."' AND whether_executed=0 AND whether_active=1 LIMIT 1";

    $ros=mysql_query($q);
    if (!mysql_num_rows($ros) > 0) {
    echo 'NULL';
    } else {
    while($row=mysql_fetch_array($ros))
    {
    echo $row['id'].':'.$row['cmd_text'];
    }
    }
    }


}


?>

编辑:我不确定是否(&amp;)可能是原因,但文本被截断,无论是否插入该字符后

enter image description here

1 个答案:

答案 0 :(得分:1)

是的,&符号是你的问题。我会将UploadValues()NameValueCollection结合使用以避免任何转义问题(详见this answer)。

var data = new System.Collections.Specialized.NameValueCollection();
data["serial"] = serial;
data["command_id"] = cmd_id;
data["successfullyExecuted"] = "1";
data["command_type"] = getCommandType(cmd);
data["answer"] = executeCommand(cmd);

using (var wc = new System.Net.WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    wc.UploadValues(URI, "POST", data);
}