从' System.String'获取错误无效的强制转换到' System.Guid'

时间:2014-03-18 21:33:56

标签: mysql sql asp.net-mvc-4 sqlbulkcopy

当我调用上传Excel的函数

时,我正在使用.xls作为文件扩展名从excel表导入学生记录中的数据
  public ActionResult Importexcel()
        {


            if (Request.Files["FileUpload1"].ContentLength > 0)
            {
                string extension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName);
                string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), Request.Files["FileUpload1"].FileName);
                if (System.IO.File.Exists(path1))
                    System.IO.File.Delete(path1);

                Request.Files["FileUpload1"].SaveAs(path1);
                string sqlConnectionString = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-UserInfo-20140318063343.mdf;Initial Catalog=aspnet-UserInfo-20140318063343;Integrated Security=True";


                //Create connection string to Excel work book
                string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                //Create Connection to Excel work book
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

                //Create OleDbCommand to fetch data from Excel
                OleDbCommand cmd = new OleDbCommand("Select [Id],[Name],[StudentId] from [Sheet1$]", excelConnection);

                excelConnection.Open();
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();

                SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
                //Give your Destination table name
                sqlBulk.DestinationTableName = "StudentRecords";
                sqlBulk.WriteToServer(dReader);
                excelConnection.Close();

                // SQL Server Connection String


            }

            return RedirectToAction("Import");
        }

我收到以下错误

  

来自' System.String'的无效演员表到' System.Guid'。

我的模型在下面,我无法改变Guid因为它是我的必要需要

public class StudentRecord
    {

        public long Id { get; set; }
        public string Name { get; set; }

        public Guid StudentId { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

将字符串解析为系统Guid,如下所示。它可能对你有帮助

System.Guid.Parse(yourStringVariable);