从SQL本地数据库返回值到全局双变量

时间:2015-08-04 17:16:03

标签: c# sql sql-server-ce

我试图从SQL Server本地数据库中获取数据。我创建了一个包含1列buyEURrate类型float的表,表名是CurrencyRates我有全局双变量,我尝试将其值设置为与FormLoad上CurrencyRates中buyEURrate列的第一行相同,所以下面的代码如下在FormLoad中。我是C#的新手。

我使用的是Visual Studio 2012,这是Windows Forms App

 using (SqlCeConnection conn =
    new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf"))
    using (SqlCeCommand cmd = conn.CreateCommand())
    {
        conn.Open();
        //commands represent a query or a stored procedure       
        cmd.CommandText = "SELECT buyEURrate FROM CurrencyRates";
        using (SqlCeDataReader rd = cmd.ExecuteReader())
        {
          while (rd.Read())
                {
                    EURbuy = Convert.ToDouble(rd[0]);
                    EURsell = Convert.ToDouble(rd[1]);
                     //Index was outside the bounds of the array. error

                }Table
        }
        conn.Close();
    }

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

using (SqlCeConnection conn =
new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf"))
using (SqlCeCommand cmd = conn.CreateCommand())
{
    conn.Open();
    //commands represent a query or a stored procedure       
    cmd.CommandText = "SELECT buyEURrate FROM CurrencyRates";
    using (SqlCeDataReader rd = cmd.ExecuteReader())
    {
       while(rd.Read())
       {
           double variable = Convert.ToDouble(rd[0]);
           string foo = rd.GetString[1];
           //do something with variable and foo before going to next row in query
       }
    }
    conn.Close();
}

这可能也有效

double variable = rd.GetDouble(0);

虽然我并非100%确定GetDouble是一个像 GetString()这样的实际功能

修改
仔细检查后double variable = rd.GetDouble(0)将第一列数据拉为双重

答案 1 :(得分:0)

using (SqlCeConnection  conn = new SqlCeConnection ("Server=.;Database=Northwind;User Id=sa;Password=as"))
{
    using (SqlCeCommand  cmd = conn.CreateCommand())
    {
        conn.Open();
        //commands represent a query or a stored procedure       
        cmd.CommandText = "SELECT buyEURrate FROM CurrencyRates";
        using (SqlCeDataReader rd = cmd.ExecuteReader())
        {
            double rate = 0;
            if (rd.Read()) // if datareader has rows
            {
                rate = Convert.ToDouble(rd[0]);
            }
        }
        conn.Close();
    }
}

答案 2 :(得分:0)

试试这个:

 using (SqlCeConnection conn =
new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf"))
using (SqlCeCommand cmd = conn.CreateCommand())
{
    conn.Open();
    //commands represent a query or a stored procedure       
    cmd.CommandText = "SELECT buyEURrate FROM CurrencyRates";
    using (SqlCeDataReader rd = cmd.ExecuteReader())
    {
         if(rd.Read())
        {
           double variable = rd.GetDouble(0);
        }
    }
    conn.Close();
}

首先需要使用DataReader读取元组,如果返回true,则可以继续读取实际数据。

如果您非常确定要读取表格第一个元组的第一列,也可以使用cmd.ExecuteScalar()方法。

以下是MSDN

的更多信息