问题
Total beginner jumping in the deep end. This is my first post here.
I'm a mainframe programmer trying to join the 1990's and learn some object oriented programming(which is currently greek to me). Please forgive my elementary explanation below.
I'm developing an application which needs to keep an availability calendar for people on a Sql Server DB. Since I could have tens of thousands (or more) rows, I was hoping to be as efficient as possible and not waste space.
Being totally green to C#, I came across BitVector 32 and realized that if I simply ignore the 0 bit, I'll have a Boolean value (True/False to describe each persons availability) for every day of the month (32 bits for 31 days in the month, with some left over for February, etc).
BitVector32 availability = new BitVector32();
This handles one month, but I could then create an array of 12 bitvector32 values, and have the entire year covered in 48 bytes (bitvector32 utilizes 4 bytes X 12 months).
I have no problem instantiating and populating a bitvector32 structure by isolating individual bits. I then pass availability.data to a method to write it out to a Sql Server Table defined as an Int value:
UpdateDB(key1, key2, availability.Data);
static void UpdateDB(int key1, int key2, Int32 avail)
{
using (SqlConnection connection = new SqlConnection etc etc etc)
{
connection.Open();
string selectString = @"Update VDB SET AVAILABILITYMONTH = @avail WHERE KEY1 = @key1 AND KEY2 = @key2";
SqlCommand cmd = new SqlCommand(selectString, connection);
cmd.Parameters.AddWithValue("@key1", key1);
cmd.Parameters.AddWithValue("@key2", key2);
cmd.Parameters.AddWithValue("@avail", avail);
cmd.ExecuteNonQuery();
connection.Close();
}
}
My problem is my next program where I need to read this integer value and convert or cast(?) it back to the Bitvector32 format.
I've tried this:
BitVector32 availability = new BitVector32();
availability = myReader.GetInt32(0);
but I get an error "Cannot implicitly convert type 'int' to 'System.Collections.Specialized.Bitvector32"
I'm right on the edge of nailing this process and I suspect it's a simple keyword or cast but I haven't been able to find it. Any ideas?
Also, is this the right approach? I didn't want to do a Boolean array for the entire year that would take up excess space.
Any input would be appreciated.
回答1:
You may use a BitVector32
constructor which takes int
argument:
BitVector32 availability = new BitVector32(myReader.GetInt32(0));
回答2:
I think you're looking for this;
BitVector32 availability = new BitVector32(myReader.GetInt32(0));
BitVector32
has a constructor which takes an Int32
however you can't just assign an Int32
to a BitVector32
because they are not the same type and C# doe not do implicit conversions like that.
来源:https://stackoverflow.com/questions/25516510/how-do-i-convert-a-sql-server-int-field-to-bitvector32-within-my-c-sharp-pro