|
Not too bad, really.
The fields sizes (or at least, safe field sizes) can be inferred from the datatypes Excel returns - have a look at the code at the bottom of the link in my original post, it works pretty well.
The names can be retrieved as the column names from SQL - and nearly everybody uses those!
You can't assign primary keys automatically - excel doesn't have the concept - so the table is going to need some manual post processing. To be expected I guess! Exactly the same with indexes and foreign key relationships etc. - again Excel doesn't know what they are.
But for getting the basic structure right, and filling it in, then transferring a working structure complete with sample data it's not a bad idea, and could well be a lot easier than the equivalent in SSMS (which tends to annoy me a lot more than it should if I tweak my data when I'm designing data storage )
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Good morning wise ones.
You've probably seen DB code akin to this before:
public List<int> GetSomeNumbers()
{
List<int> x = new List<int>();
using (SqlConnection)
using (SqlCommand)
using (SqlDataReader)
while (reader.Read()) x.Add(reader[0]);
return x;
}
Which would populate and return a list from the first column of the query. I've left the detail out, and yes a cast would be required. Another possibly better approach would be this:
public IEnumerable<int> GetSomeNumbers()
{
List<int> x = new List<int>();
using (SqlConnection)
using (SqlCommand)
using (SqlDataReader)
while (reader.Read()) yield return reader[0];
return x;
}
The reason it might be better is because it immediately returns on each read so that if I do something like send the result straight over a network I can do this is a 'streaming' approach which would be faster and not require as much memory. The reason it might not be as good is that IEnumerable is inherently more confusing. The call doesn't even get invoked until something tries to iterate it and that can catch us all out.
I'm starting to adopt the latter approach more and more, but questions arise because of the more complicated execution flow. In particular, what happens if I do this:
foreach (int x in GetSomeNumbers())
{
if (x == 0) break;
}
This is the bit I'm unsure about. Assume that a zero arrives a long time before then end of the result set. IEnumerables tell us when we're at the end of can give us the next item, but what happens if the consumer bails out before the end? There's no way (that I know of) to tell the iterator function we've concluded. Where does this leave the disposal of the reader, command and connection?
Be happy, its Friday!
Regards,
Rob Philpott.
|
|
|
|
|
Looking at section 8.8.4 of the C# Language Specification[^]:
A foreach statement of the form
foreach (V v in x) embedded-statement
is then expanded to:
{
E e = ((C)(x)).GetEnumerator();
try {
while (e.MoveNext()) {
V v = (V)(T)e.Current;
embedded-statement
}
}
finally {
}
}
Therefore, regardless of whether your statement block breaks the loop, throws an exception, or runs to completion, the enumerator object will always be disposed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Very nice answer Richard.
I had a feeling that IDisposable may be at work. Unfortunately I tend to think along the lines that an IEnumerable is merely something you iterate over rather than something that returns a disposable iterator (or enumerator if you prefer - I think the parlance is different for .NET than STL etc.)
So I guess what's happening now is the when the iterator gets disposed, it's essentially doing the same as a yield break to force its way out my own while loop. The implicitness of that bothers me slightly, because my condition of .Read() will always go to the end of the result set. And in fact the ability to break the loop is being delegated to outside.
I really should try all this out in the debugger.
So that's cleared that up and I've got one less thing to worry about now. Thanks!
Regards,
Rob Philpott.
|
|
|
|
|
Nice Answer to allow ones learning for a friday thanks
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Nice Question to improve ones learning for a friday thanks
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Rob Philpott wrote: I can do this is a 'streaming' approach which would be faster and not require as much memory.
Of course if that is a concern then caching the results would be far better than either choice.
|
|
|
|
|
I need to display data in gridview is Sharepoint Gridview is good control for that..
should i use it with Visual webpart or only webpart ?
|
|
|
|
|
|
I am talking about SPGridView
|
|
|
|
|
And what is your question?
|
|
|
|
|
How to have windows app c# in adobe flash player? Like this link
[^]
|
|
|
|
|
That's very interesting: I assume that's a simulation of a very complex .NET app; since, to my knowledge, you simply can't run a .NET App in Flash, or convert one to Flash.
But, the mind boggles: who would have the time to create such a simulation ?
I'd like to know how this was done, also !
“The best hope is that one of these days the Ground will get disgusted enough just to walk away ~ leaving people with nothing more to stand ON than what they have so bloody well stood FOR up to now.” Kenneth Patchen, Poet
|
|
|
|
|
What makes you think that this is a windows app written in C#? I see nothing in there that indicates it is any such thing.
|
|
|
|
|
But how to create such this
|
|
|
|
|
"Learn Flash".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
What is it you want to create?
|
|
|
|
|
It's not a Windows app. It's all Flash.
/ravi
|
|
|
|
|
Hi im not sure if I can ask for help here, my appolagise if not I will remove if required to.
I am stuck however with a hangman style game I am building, Simply a word pulls from file at random and the output to the user is '*' symbols instead of the letters, the user can select to guess a letter from the keyboard etc... The program does cross ref the guess with the actual word and if the user guesses correctly it will replace the '*' with the correct letter, the issue is it will only do one loop so wont allow for continuation until the word is complete. I would appreciate any help with this so that the user keeps going until the word is completed. Many Thanks. Here is the code:
private void chckGss_Func(string wordToGuess)
{
StringBuilder showUser = new StringBuilder(fileWord.Length);
for (int i = 0; i < fileWord.Length; i++)
showUser.Append('*');
List<char> goodGuess = new List<char>();
List<char> badGuess = new List<char>();
int lives = 5;
bool win = false;
int charReveal = 0;
string inpt;
char guess;
while (!win && lives > 0)
{
inpt = tmpLttr.ToUpper();
guess = inpt[0];
if (goodGuess.Contains(guess))
{
btnCorrect.Visible = true;
lblCorrect.Visible = true;
continue;
}
else if (badGuess.Contains(guess))
{
btnWrong.Visible = true;
lblWrong.Visible = true;
continue;
}
if (wrd2GuessUpper.Contains(guess))
{
goodGuess.Add(guess);
for (int i = 0; i < fileWord.Length; i++)
{
if (wrd2GuessUpper[i] == guess)
{
showUser[i] = fileWord[i];
charReveal++;
}
}
if (charReveal == fileWord.Length)
win = true;
}
else
{
badGuess.Add(guess);
lives--;
return;
}
txtWrdToGuess.AppendText(showUser.ToString());
}
if (win)
MessageBox.Show("You win!");
else
MessageBox.Show("You lose, the word was " + fileWord);
}
|
|
|
|
|
I think the problem is that return statement near the bottom. Replace it with a continue statement and I think it will work.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Iv already tried that, i heard last night that apparently it may well be the while statement thats causing the issue so ill have a look at that today
|
|
|
|
|
Hello,
I have a DLL that is a linked reference in my C# project. I would like to test for its existance before opening my form using something like "File.Exists()". I cannot find a place in my program to put the file test before an exception is thrown when the DLL is missing.
The following code works if the DLL resides in the directory with the EXE. If the DLL is missing an exception is thrown.
static void Main()
{
string message_out = (File.Exists(USBClassLibrary.dll) ? "DLL found." : "DLL missing");
MessageBox.Show(message_out);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
I thought that referenced DLLs were loaded on demand so I am puzzled by this error.
Thanks,
SMW
|
|
|
|
|
Stephen Wickland wrote: I thought that referenced DLLs were loaded on demand so I am puzzled by this
error. They are, but it's the environment that does it. If you add a reference to the project, then it has to be there in order to run.
Two alternatives; test from another program, and launch either an exe with or one without a reference. The other alternative is to load the assembly yourself, and instantiate the types yourself. Then you'll also have a statement in your code that loads the DLL, and then you can skip loading.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks BP,
Not the answer I was hoping for but I now understand the situation and can move ahead.
SMW
|
|
|
|
|
You can probably have another program to an existence test before running the application.
|
|
|
|