- SqlReader - reads one line of a table at a time, after a SELECT statement is made
- SqlDataAdapter -still learning about this. appears to be a magic box where things just happen.
- Linq - DataContext - uses high level LINQ statements to do the query. Has the advantage of mostly keeping things tightly typed. Has the disadvantage that it doesn't do generic SQL commands (like add a new column to a table).
Tuesday, June 28, 2011
Learning about SQL
There are more than one way to do queries in SQL in .NET. I'm summarizing them here for my own use:
Friday, June 11, 2010
C# example of directly getting the local time from a Domain Controller
In case you need to get the current time (appears to be UTC) from a trusted source rather than rely on the local computer to report the time (which can be easily changed), this snippet gives guidance. You can get the name of a domain controller from the LOGONSERVER environment variable.
using System.DirectoryServices.ActiveDirectory;
DirectoryContext context = new
DirectoryContext(DirectoryContextType.DirectoryServer, "insert_domain_controller_name_here");
DomainController dc =
DomainController.GetDomainController(context);
DateTime dt = dc.CurrentTime;
MessageBox.Show("Domain Time is " +
dt.ToLongTimeString());
Monday, August 24, 2009
Puzzler #1
unsigned char x = 0x80;
main (void) {
char y;
y = 0x80;
if (x != y) {
printf("This should not happen\n");
}
}
Why does the string "This should not happen" appear on the console? It does on VS2005 for x86, but may not on a 8051 processor.
Answer: The problem is that 'char' and 'unsigned char' are different when it comes to comparisons, and that compilers like to optimize the comparison using machine register sizes. C casts the operands to machine sizes: unsigned char 0x80 gets cast to 0x00000080, and signed char 0x80 gets cast to 0xFFFFFF80. Then when you compare them, they don't equate! ARGH....
Thursday, August 20, 2009
Using Bits in 8051 assembly
warning: this is a placeholder written from memory. I need to update it
Useful commands: The JB (Jump if Bit set) will jump if a bit is set. The encoding of the field is the bit offset from data memory address 0x20. If you forget this, and try encoding the bit directly, then you will actually get bit 0x20.0 + 0x20 = 0x24.0 (ie 32 bits down the line).
To declare the bit in ASM:
BSEG 0 ; offset 0 in the 0x20.0 to 2F.7 range.
BIT mybit ; declare mybit at 0x20.0
...
; useful 8-byte entry in the vector table (ie at offset 0x0003 in code space for an ISR)
CSEG AT 0x0003
JB mybit, passhop ; 3 bytes encoding
AJMP fail ; 2 bytes encoding
passhop: LJMP pass ; 3 bytes encoding = 8 bytes total
In C, you would declare the bit as:
extern bit myBit at 0x20;
How to read high resolution system time in Win32
// example of how to read the system clock.
SYSTEMTIME st;
::GetSystemTime(&st);
FILETIME ft;
::SystemTimeToFileTime(&st,&ft);
DWORD diff = ft.dwLowDateTime - lasttime;
double rate;
rate = DEF_BUF_SIZE * 1000.0 / (diff * 1e-7) ;
std::cout << " rate:" << rate << std::endl;
lasttime = ft.dwLowDateTime;
Wednesday, April 29, 2009
Memory Barriers in multi-threaded applications
Sometimes, the compiler can get in the way and break your code because it makes single-thread assumptions about your code. In addition, the microprocessors can break your code because it makes single-core assumptions. The way to fix this is use a memory barrier.
Here is a very good description of the problem of multi-threading.
Here is how to do it on a PowerPC, using the lwsync instruction.
Here is a very good description of the problem of multi-threading.
Here is how to do it on a PowerPC, using the lwsync instruction.
volatile unsigned variable1 = 0;
#define barrier() __asm__ volatile (”lwsync”)
#define ITERATIONS 50000000
void *writer(volatile unsigned *variable2) {
utilBindThreadToCPU(0);
for (;;) {
variable1 = variable1 + 1;
barrier();
*variable2 = *variable2 + 1;
}
return NULL;
}
How to predeclare typedefs for self-referential typedefs
Sometimes you need to have a typedef that refers to itself through a struct member, or there is a circular reference. Here is how to predeclare the typedef so you can use it before it is fully defined. It's basically 3 parts. First declare the struct, then declare the typedef, then define the struct using the typedef.
struct fileinfo;
struct cached_block;
typedef struct fileinfo FILEINFO;
typedef struct cached_block CACHED_BLOCK;
struct fileinfo {
FILEINFO *fi_next; /* list of all files */
CACHED_BLOCK *fi_blks; /* cached blocks for this file */
/* ... */
};
struct cached_block {
int cb_lbn; /* logical block number */
CACHED_BLOCK *cb_next; /* next cached block for this file */
FILEINFO *cb_file; /* file containing this block */
/* ... */
};
Subscribe to:
Posts (Atom)