asp.net 如何从datable中检索每一行并将每一行写入单独的文本文件?

xfb7svmp  于 2023-02-06  发布在  .NET
关注(0)|答案(1)|浏览(130)

我想将所选行检索到一个单独的文本文件中,但我的代码只检索了所有行,而不是所选行
我想知道是否有一种方法可以检查我想要检索的行,我的意思是我想要从行中进行选择
我显示了数据表,希望按选择进行选择
请问我怎么做选择过程???

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <!-- <th>-</th>-->
                <th>DATE</th>
                <th>ORDBUS</th>
                <th>ORDSER</th>
                <th>SEQUEN</th>
                <th>HEUDEB</th>
                <th>HEUFIN</th>

                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach (DataRow row in Model.Tables[0].Rows)
            {
                <tr class="active-row">
                    <!-- <td><input type="checkbox" /> </td>-->
                    <td>@row["DATMVT"]</td>
                    <td>@row["ORDBUS"]</td>
                    <td>@row["ORDSER"]</td>
                    <td>@row["SEQUEN"]</td>
                    <td>@row["HEUDEB"]</td>
                    <td>@row["HEUFIN"]</td>

                    <td>
                        <button type="submit" class="btn btn-primary" value=Edit>EDIT</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <script> $("#btnGetData").click(function () {
            var selData = table.rows(".selected").data();
            alert(selData.length);
        });</script>
   
    @using (Html.BeginForm("ConvertToTextfile", "Display", FormMethod.Get))
    {
        <input type="submit" value="Submit" class="btn btn-success" id="btnGetData" />
    }

这就是

public ActionResult ConvertToTextfile()

        {
            string mySQL;
            DataTable table = new DataTable();
            DataSet myDataSet = new DataSet();
            mySQL = "Select * From MOUVEMSSE.Mouvem11 ";
            string connect = System.Configuration.ConfigurationManager.AppSettings["dsn_AS400"];
            string conn_string = connect + "UID=" + HttpContext.Session["Username"] + ";PWD=" + HttpContext.Session["Password"] + ";";

            db_tools.sg_connStr = conn_string;
            connection = db_tools.Connect();
            table = db_tools.ExecuteSelectCommand(mySQL, CommandType.Text);
            myDataSet.Tables.Add();

            string folderLocation = @"C:\Users\zarai\source\repos\Sms_Chauffeur\Sms_Chauffeur\Extractedfiles"; // your path to save the files 

            for (int i = 0; i<table.Rows.Count; i++)  // iterate each rows to get the value
           {
               string fileName = folderLocation + "\\" + "File-" + i + ".txt";  // file name for saving each row
               string contents = "";
               foreach (DataColumn col in table.Columns)  // iteate each column to get its corresponding value
                   contents += table.Rows[i][col].ToString() + "    ";  // concatenating the contents

               System.IO.File.WriteAllText(fileName, contents);  // create a text file and copy the contents
           }

            return View("data Extracted sucssessfuly");
        }
this is the controller

enter image description here这是输出

xtfmy6hx

xtfmy6hx1#

您需要修改代码,以便只从表中检索选定的行并将其保存在文本文件中。为此,您需要使用JavaScript获取选定的行数据,并将其传递到服务器进行保存。以下是代码的更新版本:
查看:

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <!-- <th>-</th>-->
            <th>DATE</th>
            <th>ORDBUS</th>
            <th>ORDSER</th>
            <th>SEQUEN</th>
            <th>HEUDEB</th>
            <th>HEUFIN</th>

            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in Model.Tables[0].Rows)
        {
            <tr class="active-row">
                <!-- <td><input type="checkbox" /> </td>-->
                <td>@row["DATMVT"]</td>
                <td>@row["ORDBUS"]</td>
                <td>@row["ORDSER"]</td>
                <td>@row["SEQUEN"]</td>
                <td>@row["HEUDEB"]</td>
                <td>@row["HEUFIN"]</td>

                <td>
                    <button type="submit" class="btn btn-primary" value=Edit>EDIT</button>
                </td>
            </tr>
        }
    </tbody>
</table>
<script>
    $(document).ready(function() {
        var table = $('#example').DataTable();
        $("#btnGetData").click(function () {
            var selData = table.rows(".selected").data();
            if (selData.length > 0) {
                var data = JSON.stringify(selData[0]);  // get the selected row data as a string
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("ConvertToTextfile", "Display")",
                    data: { rowData: data },  // pass the selected row data to the server
                    success: function(result) {
                        alert("Data extracted successfully.");
                    }
                });
            } else {
                alert("Please select a row first.");
            }
        });
    });
</script>

@using (Html.BeginForm("ConvertToTextfile", "Display", FormMethod.Get))
{
    <input type="submit" value="Submit" class="btn btn-success" id="btnGetData" />
}

控制器:

[HttpPost]

公共操作结果转换为文本文件(字符串行数据){数据行选定行= jsonConvert.反序列化对象(行数据);//将字符串转换回DataRow

string folderLocation = @"C:\Users\zarai\source\repos\Sms_Chauffeur\Sms_Chauffeur\Extractedfiles"; // your path to save the files

相关问题