C#2D双打和字符串数组

时间:2013-09-30 18:24:13

标签: c# ado.net excel-2010

我试图在C#中的数组中获得相当大的SQL查询。但是,查询中的值由双精度数和字符串组成。我该如何解释?因为,使用下面的方法(只是将所有内容都放在一个字符串中)在我的工作表中不起作用,因为数字被格式化为文本。

String sql = "SELECT Ticker, Cusip, Shares, value, Price, " +....

string[,] data = new string[5000, 10]; //multi-dimentional array

string connectionString = Database.ConnectionString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand(sql, connection))
    {

        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            String ticker = (string)reader["ActTicker"];
            String cusip = (string)reader["Cusip9"];
            double shares = (double)reader["shares"];
            double price = (double)reader["price"];
            double value = (double)reader["value"];

            data[row, 0] = ticker;
            data[row, 1] = "=iferror(bdp(\"" + cusip + " cusip\", \"GICS_SECTOR_NAME\"),0)";
            data[row, 2] = "=iferror(bdp(\"" + cusip + " cusip\", \"SECURITY_TYP\"),0)";
            data[row, 3] = "=iferror(bdp(\"" + cusip + " cusip\", \"CUR_MKT_CAP\"),0)";
            data[row, 4] = "=iferror(bdp(\"" + cusip + " cusip\", \"VOLUME_AVG_10D\"),0)";
            data[row, 5] = shares.ToString();
            data[row, 6] = value.ToString();
            data[row, 7] = price.ToString();
            data[row, 8] = "=iferror(bdp(\"" + cusip + " cusip\", \"last price\"),0)";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用DataTable,或者如果您想要更轻量级和强类型的内容,则可以创建自己的自定义数据结构。

public struct MyCustomData 
{
    String ticker;
    String cusip;
    double shares;
    double price;
    double value;
}

使用

创建一个数组
MyCustomData[] data = new MyCustomData[5000];
.
.
.
data[row].ticker = ticker;
data[row].cusip = "=iferror(bdp(\"" + cusip + " cusip\", \"GICS_SECTOR_NAME\"),0)";
.
.
.

答案 1 :(得分:0)

    public SqlConnection myConnection = new SqlConnection("");

    public DataTable Select(string sTSQL)
    {
        DataTable dt = new DataTable();
        try
        {
            myConnection.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand(sTSQL,  myConnection);
            myReader = myCommand.ExecuteReader();

            dt.Load(myReader);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            dt = null;
        }
        finally
        {
            try
            {
                myConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        return dt;
    }