我收到错误将值读入Array [英] am getting error Reading the values into Array

查看:60
本文介绍了我收到错误将值读入Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Collections;
using System .Collections.Generic ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using System.ComponentModel;
 
public partial class _Default : System.Web.UI.Page
{
    //class Point  { double X, Y; }
    DataTable dt = new DataTable();
    //int[][] polygon=new int[][];
   
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string connectionString = "";
        if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string fileLocation = Server.MapPath("~/App_Data/" + fileName);
            FileUpload1.SaveAs(fileLocation);
            if (fileExtension == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fileExtension == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            ArrayList List = new ArrayList();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            DataTable dtExcelRecords = new DataTable();
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            if (Session["dtInSession"] != null)
            {
                dt = (DataTable)Session["dtInSession"];
            }
            for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
            {
               int result;
                //if you want to get the string
              DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
              double[] X = Convert.ToDouble(thisRow["X"]);
              double[] Y = Convert.ToDouble(thisRow["Y"]);

              polygonArea(X, Y);
            }
 
        }
    }
    //calculation
    private Double polygonArea(double[] X,double[] Y)
    {
       
        Double area = 0.0;
        int j = X.Length - 1;
        for (int i = 0; i < X.Length; i++)
        {
            area = area + (X[j] + X[i] * (Y[j] - Y[i]));
            j = i;
        }
        area = area / 2;
        if (area < 0)
            area = area * -1;
        txtArea.Text=area.ToString() ;
        return area;
    }
 
}

推荐答案

Convert.ToDouble返回(顾名思义)一个双价值 - 而不是一系列双打。

所以当你试着写:

Convert.ToDouble returns (as the name suggests) a double value - not an array of doubles.
So when you try to write:
double[] X = Convert.ToDouble(thisRow["X"]);
double[] Y = Convert.ToDouble(thisRow["Y"]);



编译器总是会抱怨不能将每个行的类型'double'隐式转换为'double []',因为你无法将double转换为一个双打数组,没有创建一个新的双精度数组,只保存一个双精度值并将其设置为转换后的值!


The compiler will always complain "Cannot implicitly convert type 'double' to 'double[]'" for each line, because you cannot convert a double to an array of doubles without crating a new array of doubles holding a single double value and setting it to the converted value!


这篇关于我收到错误将值读入Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆