|
Hi,
I have date data supplied as a string in the following format...
"Mon Sep 9 00:00:00 UTC 0100 2013".
Firstly, what is this format?
Secondly, how can I use C# to convert it and stire it in a DateTime variable?
I've tried...
DateTime dt = Convert.ToDateTime("Mon Sep 9 00:00:00 UTC 0100 2013"); but this blows up
Many Thanks.
|
|
|
|
|
Also tried...
DateTime dt = DateTime.ParseExact("Mon Sep 9 00:00:00 UTC 0100 2013", "ddd MMM dd HH:mm:ss K yyyy", CultureInfo.InvariantCulture);
Doesn't work either...
|
|
|
|
|
See here[^]
it lists, d, D, f, F, g, G, M, m, O, o, R, r, S, t, T, u, U, y, Y as valid format strings, and below the table it says : Any other single character unknown specifier. Throws a run-time FormatException.
In my case it says: String was not recognized as a valid DateTime.
try to remove the K specification along with the 0100...
|
|
|
|
|
Thanks for the reply.
I can't get it to work as a UTC. If I change it to UTC+ by adding the plus sign in to replace the space directly after the UTC on the fly then parse I can get it to work...
string strStart = "Mon Sep 9 00:00:00 UTC 0100 2013";
strStart = strStart.Replace("UTC ", "UTC+");
DateTime dtStart = DateTime.ParseExact(strStart, "ddd MMM d HH:mm:ss UTCzzzz yyyy", CultureInfo.InvariantCulture);
This works!
|
|
|
|
|
|
So I've been stuck here for 1 week.
Everytime I had problem in SQL connection (ex. someone switch of server) my application will show exception message in a messagebox. I don't know when will the connection available except I keep trying to open the connection / execute a query.
So I create a wait form that will appear if connection is unavailable, keep trying to open the connection, and close itself when connection is available again.
To hide the freeze from user, I use backgroundworker.
This is the backgroundworker code
private void StartLoader(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10; i++)
{
if (this.par.sqSuccess)
{
break;
}
else
{
i -= 1;
}
System.Threading.Thread.Sleep(5000);
}
This is the backgroundworker progress changed event
this.cnn = new SqlConnection(this.par.Constr);
try
{
this.cnn.Open();
this.par.sqSuccess = true;
}
catch (Exception ex)
{
this.par.Exception = ex.Message;
}
finally
{
if (this.cnn != null) { this.cnn.Dispose(); }
}
if (this.par.sqSuccess) { this.Close(); }
After everything is complete, I tried to stop SQL service from services.msc, then I try to connect.
The wait form will appear and keep doing its job.
A few second after I try to connect, I start the service again and the wait form did close, success.
This is the problem, when I wait a little bit longer before I start the service again, the wait form still closed, but it takes a while.
After I check everything, it seems like the cnn.open() queue up and the longer I stop the service, the longer it takes for the wait form to close.
I searched google and try to add Connect Timeout=3; behind my connection string, as I'm sure my thread.sleep(5000) won't make them queue up, but still not working.
I need help to understand how this cnn.open works, why this is not working, what happen in this code that makes I got this problem.
While processing the cnn.open(), did my application execute the code below or wait for it?
Any help is appreciated, thank you.
PS :
I use do while sqSuccess == false looping, I changed it to for just now and I know it makes no difference, so ignore it and the i-=1 too.
|
|
|
|
|
You may like to try the OpenAsync [^] method.
Veni, vidi, abiit domum
|
|
|
|
|
It uses System.Threading.Task.
Is it possible if I want to use it with backgroundworker?
|
|
|
|
|
No idea, but I suggest you read the documentation, and try a few experiments.
Veni, vidi, abiit domum
|
|
|
|
|
Oh ok ok.
There are no example of code inside but I will try googling.
Thank you very much.
|
|
|
|
|
Oh cmon, I tried a lot of example and still not working.
I'm really really stuck here.
I'm not asking someone to do the code for me, at least tell me what is wrong in my code.
Please
|
|
|
|
|
Midnight Ahri wrote: I tried a lot of example and still not working. So you want us to guess a) what your code is doing, and b) what errors occur.
Midnight Ahri wrote: at least tell me what is wrong in my code. As I said before, I have no idea.
However, re-reading your original message, it would seem much more sensible to post a message to the user when the connection fails. At least then someone can phone the help desk and alert them of the problem.
Veni, vidi, abiit domum
|
|
|
|
|
Richard MacCutchan wrote: So you want us to guess a) what your code is doing, and b) what errors occur.
I've explained everything about my code.
I'm just asking about why the sql queue up.
By the way, thank you very much.
At least you teach me async and await technique.
And I appreciate that.
I'm not good at programming, so maybe I'll ask you something again.
See ya.
|
|
|
|
|
Why would you want to use BackgroundWorker when the Task is a better choice?
|
|
|
|
|
Thank you very much, I'm new to task and just found it today.
But I'll keep searching.
After calling OpenAsync, State must return Connecting until the returned Task is completed. Then, if the connection was successful, State must return Open. If the connection fails, State must return Closed.
Sorry that english is not my first language and I'm not that good in programming.
So what I understand above is like this.
this.cnn.OpenASync();
if (this.cnn.State == ConnectionState.Connecting)
{
}
Then if it's busy, then I will return; instead of OpenASync() again.
if (this.cnn.State == ConnectionState.Closed)
{
}
And if it's closed, then I assume cnn failed and finished processing.
But it's not working.
It's ok, I'll search about task.
Thank you very much btw.
|
|
|
|
|
This is what I do,
static async Task<ConnectionState> Method(SqlConnection cnn)
{
await cnn.OpenAsync();
return cnn.State;
}
private void SQLClientLoader_Load(object sender, EventArgs e)
{
do
{
this.cnn = new SqlConnection(this.par.Constr);
try
{
ConnectionState cst = Method(cnn).Result;
if (cst == ConnectionState.Open)
{
this.par.sqSuccess = true;
}
else
{
}
}
catch (Exception ex)
{
this.par.sqSuccess = false;
this.par.Exception = ex.Message;
}
finally
{
}
} while ((bool)this.par.sqSuccess != true);
}
It freeze my application everytime I this form load executed.
Should I combine Task with BackgroundWorker to prevent freeze?
Or maybe something is wrong in my code?
Can you explain to me cause I'm new and confuse of this.
|
|
|
|
|
I'm not surprised your app is freezing, you have a tight loop here that's hogging the CPU. In the Load event, you could wrap the while loop inside a Task so that the rest of the app can start up. If, however, you need the database connection immediately, then you have to block.
|
|
|
|
|
Thank you very much for your instruction,
This is what I done.
private void SQLClientLoader_Load(object sender, EventArgs e)
{
this.GetSQLState();
}
async private System.Threading.Tasks.Task<ConnectionState> GetStateAsync()
{
do
{
this.cts = new CancellationTokenSource();
this.cnn = new SqlConnection(this.par.Constr);
try
{
await Task.Delay(2500);
await this.cnn.OpenAsync(cts.Token);
}
catch (Exception ex)
{
cts.Cancel(false);
this.cnn.Dispose();
this.cts.Dispose();
}
} while (this.cnn.State != ConnectionState.Open);
return this.cnn.State;
}
async private void GetSQLState()
{
if (await GetStateAsync() == ConnectionState.Open)
{
this.Close();
}
await Task.Delay(2500);
}
It's working.
I used cancellation token, but why the OpenAsync() is not cancelled and still queue up?
Edit :
Found my solution.
Clear the connection pool, made my day guys.
Thank you helping me.
modified 13-Sep-13 21:43pm.
|
|
|
|
|
I need to Install SQL Server 2008 R2 Express Edition without the user interaction.
Basically i want to run my .msi setup along with SQL express . the user just will click once to run and both of the setup files will be installed. Please guide me ...
|
|
|
|
|
This is not a C# issue, please try a more appropriate forum.
Veni, vidi, abiit domum
|
|
|
|
|
|
No you don't, that won't happen, and the user probably wouldn't want it to happen anyway.
The user may already have SQL Server installed (perhaps a full version), on this machine or another. Or the user may want to install your software after a newer version of SQL Server is released.
Just state in the documentation that SQL Server is required and that it's up to the user to ensure that it is available.
Perhaps include a link to the Express download for users who don't already have it installed.
|
|
|
|
|
i need something like MSDE that we have for sql server 2000. in which user just click once. User don't know which options should be selected and he will just install the application and my msi package will include both of these. but when it will run the setup for sql express it require the options to be selected. but if the user is a common person and does not know more about computer then he will just require me to provide a once click setup.
|
|
|
|
|
WaseemAmin wrote: if the user is a common person
That's a big if. Better not assume your users are idiots.
Additionally, SQL Server CE[^] may be a better fit.
|
|
|
|
|
Although I'm still a newbie, I think I can safely say I'm an intermediate newbie probably really close of crossing the threshold of an advanced newbie…
I understand most things about inheritance, encapsulation, constructors, methods and parameters, properties, accessors and access modifiers (keep in mind I said, "most things")…
I know I still have a long way to go, but…
readonly
Was this created as some sort of safety and precautionary practice?
If we don't want something to change, we can use:
const
Or properties utilizing
protected or even
private
.Net I realize is always evolving, because of this… Is this the reason for the "readonly" keyword?
|
|
|
|
|