每行.NET CSV导入器

时间:2018-01-14 21:12:08

标签: c# asp.net .net asp.net-mvc

我拼凑了这个CSV解析器,我选择了所有我读过的那些oleDB就是我能理解的那个。我有一个客户端模型我认为我需要在解析器中调用它,但我不确定。我得到这个错误并认为可能是这样吗?我怎样才能参考模型?

  

System.NullReferenceException:'对象引用未设置为对象的实例。'

在这一行

using (OleDbConnection cn = new OleDbConnection(csv.ConnectionString))

我不想再在这里重写模型,虽然我不确定如何调用它。

    public ActionResult CreateBulk(HttpPostedFileBase attachmentcsv)
    {
        ConnectionStringSettings csv = ConfigurationManager.ConnectionStrings["csv"];
        using (OleDbConnection cn = new OleDbConnection(csv.ConnectionString))
        {
            cn.Open();
            using (OleDbCommand cmd = cn.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM [attachmentcsv]";
                cmd.CommandType = CommandType.Text;
                using (OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    int clientN = reader.GetOrdinal("ClientN");
                    int homePage = reader.GetOrdinal("homePage");
                    int clientEmail = reader.GetOrdinal("clientEmail");
                    int contName = reader.GetOrdinal("contName");
                    int monthlyQuota = reader.GetOrdinal("monthlyQuota");
                    int MJTopicsID = reader.GetOrdinal("MJTopicsID");

                        foreach (DbDataRecord record in reader)
                    {
                        String Strip = record.GetString(homePage).Replace("https://www.", "").Replace("http://www.", "").Replace("https://", "").Replace("http://", "").Replace("www.", "");

                        string[] URLtests = { "https://www." + Strip, "http://www." + Strip, "https://" + Strip, "http://" + Strip };
                            string[] Metric = MajesticFunctions.MajesticChecker(URLtests);

                        var newclient = new Client { clientN = record.GetString(clientN), homePage = Metric[0], clientEmail = record.GetString(clientEmail), contName = record.GetString(contName).First().ToString().ToUpper() + record.GetString(contName).Substring(1), monthlyQuota = record.GetInt32(monthlyQuota), TrustFlow = Int32.Parse(Metric[1]), CitationFlow = Int32.Parse(Metric[2]), RI = Int32.Parse(Metric[3]), MJTopicsID = record.GetInt32(contName), UserTableID = 1 };
                        db.Clients.Add(newclient);
                        db.SaveChanges();
                    }
                }
                  return Redirect("Index");
            }
        }
    }

表单视图:

@using (Html.BeginForm("CreateBulk", "Clients", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
<div class="form-group">
<label for="attachment">Select a Csv File</label>
<label class="btn btn-default btn-file">   
<input type="file" name="attachmentcsv" id="attachmentcsv" hidden>
</label>
</div>
<button type="submit" class="btn btn-primary">Upload</button>
}
</div>

客户端模型

namespace Linkofy.Models
{
public class Client
{
    public int ID { get; set; }

    [Required]
    [Display(Name = "Client")]
    public string clientN { get; set; }

    [Display(Name = "Website")]
    public string homePage{ get; set; }

    [EmailAddress]
    [Display(Name = "Contact Email")]
    public string clientEmail { get; set; }

    [Display(Name = "Contact Name")]
    public string contName { get; set; }

    [Display(Name = "Monthly")]
    public int monthlyQuota { get; set; }

    [Display(Name = "TF")]
    public int TrustFlow { get; set; }

    [Display(Name = "CF")]
    public int CitationFlow { get; set; }

    [Display(Name = "RIPs")]
    public int RI { get; set; }

    public int? MJTopicsID { get; set; }
    public virtual MJTopics MJTopics { get; set; }

    public int UserTableID { get; set; }
    public virtual UserTable UserTable { get; set; }

    public virtual ICollection<Link> Links { get; set; }
    public virtual ICollection<Status> Statuss { get; set; }
}
}

1 个答案:

答案 0 :(得分:0)

在您的配置文件(通常是App.Config或Web.config,具体取决于项目类型)中,您应该有一个connectionStrings部分:

<configuration>  
    <connectionStrings>   
        <add name="Name" providerName="System.Data.ProviderName" connectionString="Valid Connection String;" />  
    </connectionStrings>  
</configuration> 

确保您的某个连接字符串的名称为&#34; csv&#34;。您获得的空引用异常是因为在以下代码行中找不到任何内容:

ConnectionStringSettings csv = ConfigurationManager.ConnectionStrings["csv"];

因此当您尝试访问csv中的属性时:

csv.ConnectionString

发生异常。