我一直在寻找这个问题的答案,问题是我搜索的所有内容只能给我一个问题的反转版本的答案,或者导致产生更多问题然后我不得不开始。 这可能是多次回答,但搜索能力不是那么好,但这里有:
我在mvc.asp.net应用程序中工作。
我有两个插入到数据库中的一个用于excel,一个用于csv,两个都有效,但现在我需要将它们分成三个部分。第二部分,以便用户可以自己将列表中的列映射到数据库表列名称,因为不同的行将转到我稍后添加到第3部分中的sql数据库表的不同数据表,我已经有了代码但只有需要复制它,我的问题是:
我的DataTable需要一个DataSet,以便我可以将数据表从一个actionresult移动到另一个。 我已经尝试了这个Xml版本,但它没有用。
所以我只想知道如何在其中创建一个数据集,然后在另一个actionresult中获取数据集,然后从中获取数据表,我该怎么做?
(另外,这是一个附带问题,我可以在另一个问题中询问它是否有问题,但是在excel版本的情况下它是相关的,我有一个字符串fileExstension来检查文档的类型,需要包含的xls或xlsx以及实际excel文件的文件路径。我可以在DataSet中包含它还是需要使用其他方法来移动它们?)
(对自己的评论是瑞典语。请原谅。) 当前的Excel版本代码:
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Xml;
namespace BFProj2.Controllers
{
public class ExcelImportController : HomeController
{
// GET: Import
[HttpPost]
public ActionResult Import(HttpPostedFileBase file)
{
DataTable ds = new DataTable();
if (file != null && Request.Files["file"].ContentLength > 0)
{
string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["File"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["file"].SaveAs(fileLocation);
//response redirect till ny controller
//i ny controller, använd Session["aMuze.fileLocation.Excel"] för att hämta filnamnet
//Flytta till ny controller.
string excelConnectionString = string.Empty;
//Jet providern ska automatiskt kolla om det är en HDR eller inte.
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt = new DataTable();
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
List<string> ourColumns = new List<string>();
//loopa innehållet från varje fält i första raden och lägg in det i ourColumns
ourColumns.Add("HEJ");
//TODO: Ändra fileLocation i XmlWriteMode och XmlReadMode samt lägg till en ny version av den typen.
DataSet exceldata = new DataSet();
//TODO: Add fileLocation
//TODO: Add fileExstension
exceldata.Tables.Add(ds);
Session["aMuze.fileLocation.Excel"] = fileLocation;
//
//
//Borde vara här som man bryter och skickar vidare till nästa actionresult?
//
//
String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
//TODO:Lägga till varje column så att dom kan flyttas till vyn innan dom flyttas till denna innläggning i databasen.
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
string query = string.Format("Select * from [Sheet 1$]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
if (fileExtension.ToString().ToLower().Equals(".xml"))
{
string FileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(FileLocation);
}
Request.Files["FileUpload"].SaveAs(fileLocation);
XmlTextReader xmlreader = new XmlTextReader(fileLocation);
ds.ReadXml(xmlreader);
xmlreader.Close();
//Här börjar inläsningen i Databasen.
//Flytta till tredje controller.
}
for (int i = 0; i < ds.Rows.Count; i++)
{
//Change row name if the table referred to is different - Users( , , , )
string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
DateTime time = DateTime.Now;
string format = "yyyy-MM-dd HH:mm";
//Ska ställas i rätt ordning, Titel, FirstName, LastName, Email, AbstrNum, PosterTitel, Workshop
string query2 = "Insert into Users(Titel,FirstName,LastName,Email,AbstrNum,PosterTitle,Workshop,Keywords,Institution,LastActivityDate)Values('" + ds.Rows[i][0].ToString() + "','" + ds.Rows[i][1].ToString() + "','" + ds.Rows[i][2].ToString() + "','" + ds.Rows[i][3].ToString() + "','" + ds.Rows[i][4].ToString() + "','" + ds.Rows[i][5].ToString() + "','" + ds.Rows[i][6].ToString() + "','" + ds.Rows[i][7].ToString() + "','" + ds.Rows[i][8].ToString() + "','" + time.ToString(format) + "')";
con.Open();
SqlCommand cmd = new SqlCommand(query2, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
return View();
}
[HttpPost]
public ActionResult MappaColumner(DataTable ds/*HttpPostedFileBase fileLocation, DataSet exceldata, string Filterfile, string fileExtension*/)
{
//Session["aMuze.fileLocation.Excel"] = fileLocation;
//TODO: Än så länge så är bara string Filterfile och string fileExstension där som platshållare.
//TODO: Få ut DT - ds.
// for (int y = 0; y < ds.Columns.Count; y++)
//{ }
/*String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
//TODO:Lägga till varje column så att dom kan flyttas till vyn innan dom flyttas till denna innläggning i databasen.
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
string query = string.Format("Select * from [Sheet 1$]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
if (fileExtension.ToString().ToLower().Equals(".xml"))
{
string FileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(FileLocation);
}
Request.Files["FileUpload"].SaveAs(fileLocation);
XmlTextReader xmlreader = new XmlTextReader(fileLocation);
ds.ReadXml(xmlreader);
xmlreader.Close(); */
// return RedirectToAction("Index", "Home", new { Message = "The Import was a success" });
return View();
}
public ActionResult EndResult ()
{
/*for (int i = 0; i < ds.Rows.Count; i++)
{
//Change row name if the table referred to is different - Users( , , , )
string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
DateTime time = DateTime.Now;
string format = "yyyy-MM-dd HH:mm";
//Ska ställas i rätt ordning, Titel, FirstName, LastName, Email, AbstrNum, PosterTitel, Workshop
string query2 = "Insert into Users(Titel,FirstName,LastName,Email,AbstrNum,PosterTitle,Workshop,Keywords,Institution,LastActivityDate)Values('" + ds.Rows[i][0].ToString() + "','" + ds.Rows[i][1].ToString() + "','" + ds.Rows[i][2].ToString() + "','" + ds.Rows[i][3].ToString() + "','" + ds.Rows[i][4].ToString() + "','" + ds.Rows[i][5].ToString() + "','" + ds.Rows[i][6].ToString() + "','" + ds.Rows[i][7].ToString() + "','" + ds.Rows[i][8].ToString() + "','" + time.ToString(format) + "')";
con.Open();
SqlCommand cmd = new SqlCommand(query2, con);
cmd.ExecuteNonQuery();
con.Close(); */
return View();
}
}
}
当前型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace BFProj2.Models
{
public class DataSetModel
{
DataSet csvdata { get; set; }
DataSet exceldata { get; set; }
string fileExstension { get; set; }
string fileLocation { get; set; }
}
}