|
led mike wrote: Huh? I'm LINQless so I have no idea what that means.
Linq has the ability to change QueryProviders based on what is being queried. So calling Linq of a database source, will translate the Linq query into SQL. Once iterating over the query, the SQL will be executed.
So what it comes down to is that SQL should be 'strong' enough to optimize a Count(select * from table where i = 0) > 0 expression into something that resembles the Any function.
That probably does not make any sense
|
|
|
|
|
leppie wrote: That probably does not make any sense
Whew, I thought it was me.
So then where does some sort of performance gain come into effect in the current example? I mean how are the expression trees etc., going to faster than code like:
char[] chars = failure.ToCharArray();
bool bad = false;
for (int n = 0; !bad && n < chars.Length; n++)
if (!Char.IsDigit(chars[n]) && !Char.IsWhiteSpace(chars[n]))
bad = true;
led mike
|
|
|
|
|
led mike wrote: So then where does some sort of performance gain come into effect in the current example? I mean how are the expression trees etc., going to faster than code like:
It's not. It could be equal or slower. But it's easier to write failure.Any(c => !(char.IsWhiteSpace(c) || char.IsDigit(c)))
I am willing to bet, that code would run close to your snippet.
|
|
|
|
|
leppie wrote: But it's easier to write
By easier to write you mean less key presses?
led mike
|
|
|
|
|
leppie wrote: But you can do something with same effect.
Meaning it will traverse the entire string. It will be ok for short strings, or if you do not expect many failures.
A better solution is to use Regex, IMO.
|
|
|
|
|
Damn - you beat me to it. I should have known.
|
|
|
|
|
You could do this as:
string failure = "123 512512512 1";
bool bad = (from p in failure.ToCharArray()
where !char.IsDigit(p) && !char.IsWhiteSpace(p)
select p).Count() > 0;
Console.WriteLine("{0}\n", bad); I wouldn't really recommend it though because you are going to get a lot of work going on there if the array becomes really large. What I would recommend instead is using a regular expression to handle this. While LINQ is decent at a lot of tasks, there are times when it's a lot better to use other technologies which are more suited to the task.
|
|
|
|
|
Hopefully a simple question. I am creating an output file, and when finished I need to go back and update the header row with a row count in position 10. I am using StreamWriter.
Here is a the original output file, and I would like to replace the "0000" with "0005" on line one:
Habcdefghi0000tuvwxyz
detail one
detail two
detail three
detail four
detail five
Here is the code:
FileStream fs = new FileStream("c:\\textFile.txt", FileMode.Open, FileAccess.ReadWrite);
using(StreamWriter sw = new StreamWriter(fs))
{
Console.WriteLine("Current Position: {0}",sw.BaseStream.Position);
sw.BaseStream.Seek(10, SeekOrigin.Begin);
Console.WriteLine("Current Position: {0}", sw.BaseStream.Position);
sw.Write("0005");
}
That works - header now looks as follows:
Habcdefghi0005tuvwxyz
However, when I try to update another value on the header (at position 1)...things get real screwy. Here is the updated code:
FileStream fs = new FileStream("c:\\textFile.txt", FileMode.Open, FileAccess.ReadWrite);
using(StreamWriter sw = new StreamWriter(fs))
{
Console.WriteLine("Current Position: {0}",sw.BaseStream.Position);
sw.BaseStream.Seek(10, SeekOrigin.Begin);
Console.WriteLine("Current Position: {0}", sw.BaseStream.Position);
sw.Write("0005");
sw.BaseStream.Seek(1, SeekOrigin.Begin);
Console.WriteLine("Current Position: {0}", sw.BaseStream.Position);
sw.Write("Test");
}
This time, the WriteLine output appears correct (positions: 0, 10, and 1), but the header looks like this:
H0005Testi0000tuvwxyz
I read somewhere that the buffers might need to get flushed/reset, but can't seem to find that method. Also, is there a better way to update the header row?
Thanks for any advice that can be offered!
|
|
|
|
|
sw.AutoFlush = true; ?
Alan.
|
|
|
|
|
|
captjack wrote: I read somewhere that the buffers might need to get flushed/reset, but can't seem to find that method.
Where did you look? Everywhere but in the StreamWriter class?
You have to use the Flush method of the StreamWriter before you move the position in the underlying stream, otherwise it may write whatever is in the buffer to the new position:
using (FileStream fs = new FileStream("c:\\textFile.txt", FileMode.Open, FileAccess.ReadWrite)) {
using(StreamWriter sw = new StreamWriter(fs)) {
sw.BaseStream.Seek(10, SeekOrigin.Begin);
sw.Write("0005");
sw.Flush();
sw.BaseStream.Seek(1, SeekOrigin.Begin);
sw.Write("Test");
}
}
Setting auto-flush (as suggested elsewhere in the thread) works, but if you use it when writing more than a few strings to a file you can reduce the performance, as a whole cluster (about 8 kiB) is written to the disk each time.
Also, you have to be very careful when changing the position to write in a text file. Characters outside the ASCII character set use more than a single byte in the file, so you can easily corrupt characters in the file by overwriting part of a multi-byte character. Be careful only to write single-byte characters and only overwrite single-byte characters.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Good Afternoon All
i think its Firday fever, am tired. I think this Happens sometimes. let me get to the Problem. Am doing an OleDb Project
I have an Access DB and the table is Users , here are the Field and dataType
ID = Autonumber(int)
Firstname = TEXT
LastName = TEXT
Username = TEXT
Password = TEXT
Email = TEXT
Now i have a Function that inserts into these Field from my DAL that is Written like this
public int Register_User(String Firstname, String LastName, String Username, String Password, String Email)
{
int Res=0;
con = new OleDbConnection(strcon);
cmdinsert = new OleDbCommand();
cmdinsert.CommandText = "insert into Users Values(?,?,?,?,?)";
cmdinsert.CommandTimeout = 0;
cmdinsert.CommandType = CommandType.Text;
cmdinsert.Connection = con;
cmdinsert.Parameters.Add("Username", OleDbType.VarChar,30).Value = Convert.ToString(Username);
cmdinsert.Parameters.Add("Password", OleDbType.VarChar,30).Value = Convert.ToString(Password);
cmdinsert.Parameters.Add("Firstname", OleDbType.VarChar,30).Value = Convert.ToString(Firstname);
cmdinsert.Parameters.Add("LastName", OleDbType.VarChar,30).Value = Convert.ToString(LastName);
cmdinsert.Parameters.Add("Email", OleDbType.VarChar, 30).Value = Convert.ToString(Email);
try
{
con.Open();
Res =(int) cmdinsert.ExecuteScalar();
}
catch (OleDbException)
{
throw;
}
finally
{
con.Close();
}
return Res;
}
When i try to insert , i get an Exception.
Number of query values and destination fields are not the same.
Can you please Point the Problem for me
Thank you
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
try changing your query to
"INSERT INTO Users(Firstname,LastName,Username,Password,Email) VALUES(?,?,?,?,?)"
|
|
|
|
|
is there any reason you are doing a
Convert.ToString(Username)
to set the value, as it is being passed in as a string anyway?
|
|
|
|
|
Just to make sure? Or bloat it?
Chuck Norris has the greatest Poker-Face of all time. He won the 1983 World Series of Poker, despite holding only a Joker, a Get out of Jail Free Monopoloy card, a 2 of clubs, 7 of spades and a green #4 card from the game UNO. In the movie "The Matrix", Chuck Norris is the Matrix. If you pay close attention in the green "falling code" scenes, you can make out the faint texture of his beard. Chuck Norris actually owns IBM. It was an extremely hostile takeover.
|
|
|
|
|
Am the Day have been long, and you get Small Error like missing semicolon
this happens "Fool Proof it".
Thanks for you Comment
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
FoolProffing it i geuse after a long day when the code gives me an Error that is Simple to solve, but because i cant see it because of the tired mind , i will try to make sure that i fool proof every possibility
Thanks for your Comment
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
hi Man
Am back with fresh mind. The Problem was the Order of Parameters and your insert statement helped.
Thanks
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
I don't use Access, but I suspect you need to list your fields so it knows to skip the id
cmdinsert.CommandText = "insert into Users(Firstname,LastName,Username,Password,Email) Values(?,?,?,?,?)";
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hallo Vuyiswa
Please look at the soluction below I have tested it and it works.
public int Register_User(String Firstname,String LastName,String Username,String Password,String Email)
{
int Res = 0 ;
OleDbCommand cmdinserts;
cmdinserts = new OleDbCommand();
con = new OleDbConnection(strcon);
cmdinserts.CommandText = "insert into [Users]([Firstname],[LastName],[Username],[Password],[Email]) Values(?,?,?,?,?)";
cmdinserts.CommandTimeout = 0;
cmdinserts.CommandType = CommandType.Text;
cmdinserts.Parameters.Add("@Firstname",OleDbType.Char,30).Value = Firstname;
cmdinserts.Parameters.Add("@LastName",OleDbType.Char,30).Value = LastName;
cmdinserts.Parameters.Add("@Username",OleDbType.Char,30).Value = Username;
cmdinserts.Parameters.Add("@Password",OleDbType.Char,30).Value = Password;
cmdinserts.Parameters.Add("@Email",OleDbType.Char,30).Value = Email;
cmdinserts.Connection = con;
try
{
con.Open();
//Res = (int)cmdinsert.ExecuteScalar();
Res = (int)cmdinserts.ExecuteNonQuery();
}
catch (OleDbException)
{
throw;
}
finally
{
con.Close();
}
return Res;
}
Hope this helps.
Ngiyabonga
Langa
|
|
|
|
|
Wow Langa,
Thanks Am Proud of you, i just came back now and Fresh again, I see that the Order of Paramaters was the Problem. They should be the Same.
Thanks
I gave you 5
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
Hi,
What is best machinsm to check whether the server is UP or not?
How far ping mechanism is reliable?
If any good machanism is there to check whether server is UP or not?
Kindly help me
|
|
|
|
|
Don't cross post, geeze.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
balu12345 wrote: What is best machinsm to check whether the server is UP or not?
I would not recommend the C# mechanism.
|
|
|
|
|
Hello all!
A silly question concerning the OSVersion. When I call the following method it returns the platform as the following;
"Microsoft Windows NT 5.1.2600 Service Pack 2"
Despite under the control panel it is listed as;
"Microsoft Windows XP Professional Version 2002 Service Pack 2"
Is there anything that maps one from the other?
Many thanks!
|
|
|
|
|