|
What happened when you tried it ?
|
|
|
|
|
I am trying to insert a new record into my database. The primary key field is an integer. How can I determine what the max value for the field is before I do my insert?
I know this is very easy if I put the insert inside of a stored proc and just do a Max() before my insert, but for this project I can not use stored procs, all the sql is in the code. Also, I know I can set the primary key to be an identity field, but I am trying to avoid that.
Here is my current code:
public bool Insert(ActionObject obj)
{
SqlCommand sp;
try
{
using (SqlConnection conn = SqlConn)
{
conn.Open();
string sql = @"INSERT
INTO [dbo].[Action] ([ActionId], [ActionName], [ActionDescription])
Values (@ActionId, @ActionName, @ActionDescription)";
sp = new SqlCommand(sql, conn);
sp.CommandType = CommandType.Text;
sp.Parameters.Add("@ActionId", SqlDbType.Int).Value = obj.ActionId;
sp.Parameters.Add("@ActionName", SqlDbType.VarChar).Value = obj.ActionName == null ? "" : obj.ActionName;
sp.Parameters.Add("@ActionDescription", SqlDbType.VarChar).Value = obj.ActionDescription == null ? "" : obj.ActionDescription;
sp.ExecuteNonQuery();
}
}
catch(Exception)
{
throw;
}
}
|
|
|
|
|
If you make the primary key column an identity column then SQL Server will work it out for you. That way you don't have to calculate it in advance - and run the risk of someone else performing the same calculation and getting in there before you.
|
|
|
|
|
I agree that would be the best solution, unfortunately these tables are already created and I can not alter their structure.
|
|
|
|
|
Then to do it safely you need to use a transaction. Have a look at the SqlTransaction class.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Seconding what Colin said, if you go with doing an identity field, you will not have to be concerned about it.
"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
|
|
|
|
|
That would be a reason not to use integers for primary keys.
|
|
|
|
|
What is the alternative? GUIDs?
|
|
|
|
|
Yes, they solve this kind of problem and others (at the cost of space, sure, but we have plenty of that now ).
But you also say you can't change the table so that's not an option here.
After I hit send on the other post I remembered that back when I used Oracle (6) there were "sequences" or something that gave the "next integer" and when the company I worked at the time started using SQL Server (6) we no longer had those and apparently not "identities" either, so we cobbled up our own "sequences" -- a table that held the necessary information to get the "next integer" for a particular table.
That may be an ancient technique to resurrect in your case.
|
|
|
|
|
Chain the GetMaxValue and the Insert in the same transaction.
Do housekeeping on the transaction log, though.
|
|
|
|
|
Normally, when you want to do this type of thing you need to create your own Sequence table, which contains the Next ID that you want to use. Then inside a transaction, you update the row to add 1 to it, and retrieve it, before committing it. You must do this in that order, so that no other user can get access to the record while you are working with it.
|
|
|
|
|
How do I do this? An example of what I'm trying to do is in MSN Messenger, when someone sends you a message and the window name flashes on your start menu bar.
Thanks.
|
|
|
|
|
You can do this using the FlashWindow() /FlashWindowEx() function. Take a look at this article.
Regards,
--Perspx
"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia
Introduction to Object-Oriented JavaScript
|
|
|
|
|
Thanks .
|
|
|
|
|
hi friends ,
I am sathish kumar ,I am developing a three tier windows application using .net framework 3.5 and sql server 2005 , I want to know how to deploy my application using windows installer , i scripted my database i want to run the script along with my installer during installation it must ask the user to choose the database instance and that must be reflected to app.config file so that my application might run in my client machine.
pls tell me help me or tell me any other better idea to deploy my application pls help me friends i am waiting for ur fav reply
pls provide me some videos and steps for deployment
|
|
|
|
|
|
Hi All,
I have a little application and a part of it has to send e-mails. Bud the problem is that when i click on send email button and the application tries to send the e-mail, my form freezes til the e-mail is out and i can't do anything else on the form.
What can i do to provide that?
Thanx.
|
|
|
|
|
NewToAspDotNet wrote: have a little application and a part of it has to send e-mails. Bud the problem is that when i click on send email button and the application tries to send the e-mail, my form freezes til the e-mail is out and i can't do anything else on the form.
Use a background thread instead to send the mail
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
|
Hi everybody,
i'm creating an application using an external liberay that creates a control.If you click on this control it reacts on the mouse click, but i want to cancel this. I want to implement my own mouse click event for that control and cancel the click message to the control. Problem is that the control receives the mouse click event as first.
I tries to use wndproc to implement my own code but still the control receive the click first.
Can this be done by using a hook?
How do i implement this and then cancel the mouse event before it reaches the control??
Thnx
|
|
|
|
|
Can you derive your control from it?
|
|
|
|
|
you can use cancel propert of the e arg that is passed to the event handler like this:
e.Cancel = true;
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]
|
|
|
|
|
No I can not use any properties of the control...
I have to receive the cliock message before it reaches the control and cancel it!
Anybody an Idea?
|
|
|
|
|
Can any one help
How to Drag and Drop the Button Control using Windows Presentation Foundation(C Sharp)
|
|
|
|
|
bruze wrote: How to Drag and Drop the Button Control using WPF
Better ask in the WPF Forum
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|