如何写入数据表?

时间:2019-06-06 22:09:13

标签: c# asp.net-mvc

这里我需要一双新鲜的眼睛。我正在尝试从用户上传的文件中提取详细信息,将它们添加到DataTable中,然后将其写入我的SQL Server表中。

看起来一切正常,但是对于将实际字段添加到DataTable而言(相当关键!)。该代码运行完毕,但是有一个空白的DataTable,因此显然没有任何内容写入我的表中。我附上了控制器-我敢肯定我很想念它,但是我无法确定原因。

public class FileUploaderController : Controller
{

    public FileUploaderController()
    {
        db = new DatabaseContext();

    }

    public FileUploaderController(DatabaseContext context)
    {
        db = context;
    }

    private DatabaseContext db { get; }

    public ActionResult UploadFilesPartial()
    {
        return View();
    }


    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {

        var dt = CreateDataTable();

        if (file != null)
        {
            var fileName = file.FileName;
            var filePath = Path.GetFullPath(fileName);
            var fileType = Path.GetExtension(fileName);

            DataRow fileDetails = dt.NewRow();
            fileDetails["FileName"] = fileName;
            fileDetails["FilePath"] = filePath;
            fileDetails["FileType"] = fileType;

            UploadToDataBase(db, dt);
        }


        return RedirectToAction("NettingBatch", "Views");

    }


    private DataTable CreateDataTable()
    {

        System.Data.DataTable table = new DataTable("FileUploads");

        DataColumn id;
        id = new DataColumn
        {
            DataType = System.Type.GetType("System.Int32"),
            ColumnName = "Id",
            ReadOnly = true,
            Unique = true
        };
        table.Columns.Add(id);

        DataColumn name;
        name = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FileName",
            AutoIncrement = false,
            Caption = "FileName",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(name);

        DataColumn path;
        path = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FilePath",
            AutoIncrement = false,
            Caption = "FilePath",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(path);

        DataColumn type;
        type = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FileType",
            AutoIncrement = false,
            Caption = "FileType",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(type);

        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = table.Columns["Id"];
        table.PrimaryKey = PrimaryKeyColumns;

        return table;

    }




    private void UploadToDataBase(DbContext context, DataTable data)
    {
        try
        {
            var columnMappings = new List<Tuple<string, string>>
            {
                new Tuple<string, string>("Id", "Id"),
                new Tuple<string, string>("FileName", "FileName"),
                new Tuple<string, string>("FilePath", "FilePath"),
                new Tuple<string, string>("FileType", "FileType"),

            };

            PopulateHistoryTable.BulkCopyDataTableIntoDatabase(context, data, "[mdp].[FileUploads]",
                columnMappings);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.InnerException);
        }



    }


}

我希望提取在视图上上传的测试文件的详细信息,并将其写回到FileUploads表中。实际结果为空白。

1 个答案:

答案 0 :(得分:1)

NewRow返回与您的DataTable方案匹配的行,但实际上并没有将其“添加”到DataTable中。使用Rows集合的“添加”功能可以做到这一点:

DataRow fileDetails = dt.NewRow();
fileDetails["FileName"] = fileName;
fileDetails["FilePath"] = filePath;
fileDetails["FileType"] = fileType;
dt.Rows.Add(fileDetails);