Mysql 从';获取错误无效强制转换;System.String';至';System.Guid';

Mysql 从';获取错误无效强制转换;System.String';至';System.Guid';,mysql,sql,asp.net-mvc-4,sqlbulkcopy,Mysql,Sql,Asp.net Mvc 4,Sqlbulkcopy,当我调用上载excel的函数时,我正在从excel工作表导入学生记录中的数据,文件扩展名为.xls public ActionResult Importexcel() { if (Request.Files["FileUpload1"].ContentLength > 0) { string extension = System.IO.Path.GetExtension(Request.

当我调用上载excel的函数时,我正在从excel工作表导入学生记录中的数据,文件扩展名为.xls

  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; }

    }
也许,您必须通过对象对sou从db获得的内容进行itarate,然后逐个解析以达到上面的语句


您还可以查看如何使用类型转换器。

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

System.Guid.Parse(yourStringVariable);

不确定对工作表的ole访问是否具有此功能,但新的OleDbCommand(“选择[Id]、[Name]、强制转换(StudentId为UniqueIdentifier)为StudentId..”可能会执行此操作
System.Guid.Parse(yourStringVariable);