|
That would be my preference. Others may suggested parameterised queries, but I like to keep as much of the actual implementation out of my code as possible - just using a stored proc name and a list of parameters seems cleaner to me than generating a sql insert statement. Also reduces network traffic.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
There's lot of ways.
Stored procedure
Parametrized query
ORM layer (Like NHibernate, LinqToSql or the Entity Framework)
A lot of people will argue SPs are better because the server optimises them. This is not true, modern database servers cache the execution plan for normal queries just as they do for stored procedures. It pretty much comes down to personal preference between the two. (Personally I prefer parametrized queries, I find them easier to maintain than SPs, but everyone has a different opinion on this)
An ORM is likely to be slightly slower, but will give you better abstraction and you won't have to write too much DAL code. Although they often require a lot of configuration will some form of GUI or XML based tool. Sometimes you can auto generate the ORM config by pointing it the database.
Simon
|
|
|
|
|
as in the following code?
INSERT INTO dbo.MYTABLE <br />
(<br />
prikey,<br />
col1,<br />
col2,<br />
col3,<br />
col4,<br />
)<br />
VALUES <br />
(<br />
NewId(),<br />
@Param1,<br />
@Param2,<br />
@Param3,<br />
@Param4,<br />
)
i get the following error if I use NewId()
<br />
Operand type clash: uniqueidentifier is incompatible with int
----------------------------------------------------------
"unzip; strip; touch; finger; mount; fsck; more; yes; unmount; sleep" - my daily unix command list
|
|
|
|
|
You make the primary key an identity column, and then just dont specify it:
<br />
INSERT INTO dbo.MYTABLE<br />
(<br />
col1,<br />
col2,<br />
col3,<br />
col4,<br />
)<br />
VALUES<br />
(<br />
@Param1,<br />
@Param2,<br />
@Param3,<br />
@Param4,<br />
)
newid() generates an new GUID
|
|
|
|
|
Hi, im having some real PROBLEMS implementing this algorithm, i just cant find anywhere that explains it in a simple enough manner for me to understand, so i was wondering if anyone here could knock together a quick example for me? I've had a stab at it so far and this is what i've got:
public Hashtable FindShortestPath(string start, string end)
{
Dictionary<string, Node<T>> Q = new Dictionary<string, Node<T>>();
Hashtable distance = new Hashtable();
Hashtable route = new Hashtable();
foreach (KeyValuePair<string, Node<T>> kvp in Nodes)
{
distance.Add(kvp.Key, Int32.MaxValue);
route.Add(kvp.Key, null);
Q.Add(kvp.Key, kvp.Value);
}
distance[start] = 0;
while (Q.Count >= 0)
{
string minimumID = null;
int minvalue = Int32.MaxValue;
foreach (KeyValuePair<string, Node<T>> kvp in Q)
{
if ((int)distance[kvp.Key] < minvalue)
{
minvalue = (int)distance[kvp.Key];
minimumID = kvp.Key;
}
}
foreach (Edge<T> e in EdgeList)
{
if (e.startNode.ID == minimumID)
{
int startNodeDistance = (int)distance[minimumID];
int endNodeDistance = (int)distance[e.endNode.ID];
if (startNodeDistance + e.weight < endNodeDistance)
{
distance[e.endNode.ID] = startNodeDistance + e.weight;
route[e.endNode.ID] = minimumID;
}
}
}
Q.Remove(minimumID);
}
foreach(int k in route.Keys)
{
Console.WriteLine("k: " + route[k]);
}
return route;
Can someone tell me where im going wrong please?
Many Thanks
|
|
|
|
|
|
Hello everyone,
I am writing an API which accepts request and conver the request to call to database store procedures. There may be a lot of simultaneous requests to a store procedure (the store procedure may just query for data, or may update database). My concerns is I do not want to hit database too hard and at the same time achieve the best performance.
What is the traditional solution to this problem? Using thread pool and keep connection to database alive? Or do nothing and trust database store procedure to do its own optimization?
thanks in advance,
George
|
|
|
|
|
If your using SQL server it does it's own connection pooling. It will automatically reuse existing connections if it is optimal to do so.
Simon
|
|
|
|
|
Thanks Simon!
1.
Your suggestion is I just feel free to call SQL Server and let SQL Server itself handles large number of simultaneous call?
2.
I am interested in this topic, do you have any related documents to refer?
regards,
George
|
|
|
|
|
1. Yes.
2. No, not really. Just use google.
Simon
|
|
|
|
|
Thanks Simon,
Could you provide some keywords to make a search? I searched for "C# store procesure" and it contains just basic things like how to use store procesure in C#. Not too much materials about how SQL manages store procedure when there is large number of simultaneous calls.
regards,
George
|
|
|
|
|
Just use some common sense. SQL Stored Procedure Optimise Automatic Execution Plan Caching
George_George wrote: procesure
Start by spelling "procedure" correctly
how to google[^]
Simon
|
|
|
|
|
Hi Simon,
I did some search by the keywords you mentioned and find a lot of things. Various things, like batch execution as best practice, cache plan, store procedure optimization technologies.
I am willing to follow your suggest to find the right technology to my problem, but I have found so many. Could you just recommend me the technology name or some reference link I should use? Thanks.
regards and have a good weekend,
George
|
|
|
|
|
within or instead of the entire switch statement. i was advised that using tags are better than using the index in a switch statement. however switch only accepts integer types?
thank you in advance
----------------------------------------------------------
"unzip; strip; touch; finger; mount; fsck; more; yes; unmount; sleep" - my daily unix command list
|
|
|
|
|
The Tag property is of Type 'object'. If you put integers in them, cast the Tag.
Convert.ToInt32(mytextbox.Tag);
|
|
|
|
|
Hello everyone,
I have two ways to define the constructor of my exception class, which is derived from ApplicationException class.
I am not sure the pros and cons of the two constructor definitions? Any comments or advice?
class MyException : System.ApplicationException
{
private int _errorCode;
private string _errorDescription;
public MyException (int errorCode, string errorMessage)
: base (errorMessage)
{
_errorCode = errorCode;
_errorDescription = errorMessage;
}
public MyException (int errorCode, string errorMessage)
{
_errorCode = errorCode;
_errorDescription = errorMessage;
}
}
thanks in advance,
George
|
|
|
|
|
The former constructor is preferred as it allows the exception's message to be accessible when the exception is treated as ApplicationException .
/ravi
|
|
|
|
|
Thanks Ravi,
1.
What do you mean "treated as ApplicationException"? Convert MyException type to ApplicationException?
2.
And for the latter one when treated as ApplicationException, no error message information could be fonud?
regards,
George
|
|
|
|
|
Yessir. Right on both counts.
/ravi
|
|
|
|
|
Thanks ravi!
regards,
George
|
|
|
|
|
I agree with Ravi, the former is much better. But you don't need all the lines you've got in there.
class MyException : System.ApplicationException
{
private int _errorCode;
private string _errorDescription;
public MyException (int errorCode, string errorMessage)
: base (errorMessage)
{
_errorCode = errorCode;
_errorDescription = errorMessage;
Debug.WriteLine(base.Message);
}
}
Simon
|
|
|
|
|
Cool, Simon! If I do not call base (errorMessage) in constructor, will there be any issues?
regards,
George
|
|
|
|
|
George_George wrote: If I do not call base (errorMessage) in constructor, will there be any issues?
Yes. If you don't pass the errorMessage to the base it will never get stored in the base class.
Simon
|
|
|
|
|
Thanks Simon!
1.
I have another question about [Serializable] tag applied to exception class. Is it used for serialization process only or used for both serialization/de-serialization process?
2.
If [Serializable] could control both serialization and de-serialization process, why there is another constructor which accepts SerializationInfo and StreamingContext?
http://msdn.microsoft.com/en-us/library/tz6bzkbf.aspx[^]
regards,
George
|
|
|
|
|
1. I'm not sure I understand the question. What difference does it make? Perhaps you should ask this in a separate thread away from the context of this question on exceptions. Somebody else might understand better.
2. For more precise control over the serialisation, for custom serialisation formats and schemas. Which you might need if you are planning on serialising the object for interop with another system.
Simon
|
|
|
|