简体   繁体   中英

How to get multiple variable from SQL command in set them to local variable using ExecuteScalar in c#

Using this code I get my variable from an MS Access 2013 database and store it into a local variable:

public static int UpdateDBtCowsCalculatedVariable()
{
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
    OleDbConnection Connection = new OleDbConnection(StrCon);

    OleDbCommand Cmd = new OleDbCommand();
    Cmd.Connection = Connection;
    Cmd.CommandText = "Select BW from tCows";
    Connection.Open();
    int BW = (int)Cmd.ExecuteScalar();
    Connection.Close();
        return BW;         
}

But what about multiple variables? How should I do that?

I mean something like this:

public static void UpdateDBtCowsCalculatedVariable()
{
    //int BW, DaysInMilk, AnimalType;
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
    OleDbConnection Connection = new OleDbConnection(StrCon);

    OleDbCommand Cmd = new OleDbCommand();
    Cmd.Connection = Connection;
    Cmd.CommandText = "Select BW, DaysInMilk, AnimalType from tCows";
    Connection.Open();
    int BW = (int)Cmd.ExecuteScalar();
    // int DaysInMilk = (int)Cmd.ExecuteScalar();
    // int AnimalType = (int)Cmd.ExecuteScalar();
    Connection.Close();
}

Why not ExecuteReader() ? Even if you, technically, have just one record:

public static void UpdateDBtCowsCalculatedVariable()
{
    string StrCon = ...

    int BW = -1; 
    int DaysInMilk = -1;
    int AnimalType = -1;

    using (OleDbConnection Connection = new OleDbConnection(StrCon)) 
    {
        Connection.Open();

        using (OleDbCommand Cmd = new OleDbCommand())
        {
            Cmd.Connection = Connection;
            Cmd.CommandText = 
              @"select BW, 
                       DaysInMilk, 
                       AnimalType 
                  from tCows";

            using (var reader = Cmd.ExecuteReader())
            {
                if (reader.Read()) 
                {
                    BW =         Convert.ToInt32(reader.GetValue(0));
                    DaysInMilk = Convert.ToInt32(reader.GetValue(1));
                    AnimalType = Convert.ToInt32(reader.GetValue(2));   
                }
            }

        }
    }
}

Try this:

public static void UpdateDBtCowsCalculatedVariable() {
//int BW, DaysInMilk, AnimalType;

string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);

OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = Connection;
Cmd.CommandText = "Select BW, DaysInMilk, AnimalType from tCows";
Connection.Open();
reader = Cmd.ExecuteReader();
while (reader.Read())
{
  int BW = reader.GetValue(0);
  int DaysInMilk =  reader.GetValue(1);
  int AnimalType =  reader.GetValue(2);
}
reader.Close();
Connection.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM