Return 值从 SQL 本地数据库到全局双变量
Return value from SQL local database to global double variable
我正在尝试从 SQL 服务器本地数据库获取数据。我创建了具有 1 列 buyEURrate 类型 float 的 Table 并且 table 名称是 CurrencyRates 我有全局双变量并且我试图将其值设置为与 CurrencyRates 中 buyEURrate 列的第一行相同在 FormLoad 上,所以下面的代码在 FormLoad 中。我真的是 SQL 的 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();
}
像这样的方法应该有效:
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)
将把第一列数据作为 double
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();
}
}
试试这个:
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 读取一个元组,如果它 returns 为真,您可以继续读取并获取实际数据。
如果您非常确定要准确读取 table 的第一个元组的第一列,也可以使用 cmd.ExecuteScalar() 方法。
提供更多信息
我正在尝试从 SQL 服务器本地数据库获取数据。我创建了具有 1 列 buyEURrate 类型 float 的 Table 并且 table 名称是 CurrencyRates 我有全局双变量并且我试图将其值设置为与 CurrencyRates 中 buyEURrate 列的第一行相同在 FormLoad 上,所以下面的代码在 FormLoad 中。我真的是 SQL 的 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();
}
像这样的方法应该有效:
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)
将把第一列数据作为 double
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();
}
}
试试这个:
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 读取一个元组,如果它 returns 为真,您可以继续读取并获取实际数据。
如果您非常确定要准确读取 table 的第一个元组的第一列,也可以使用 cmd.ExecuteScalar() 方法。
提供更多信息