Timeline for SQL call for a scalar
Current License: CC BY-SA 3.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 11, 2016 at 19:47 | vote | accept | Konrad Viltersten | ||
Aug 14, 2014 at 21:26 | comment | added | Malachi | @KonradViltersten, it all depends on your application. the way I suggest may not be the best way for you. | |
Aug 14, 2014 at 21:13 | comment | added | Konrad Viltersten | @Malachi Yes, it does. The way I resolved it, I create the connection elsewhere but I open and close it in the method. The reason for that is that the connection is going to be used several times during a longer time, so it needs to exist as a private property. However, it's not going to be actively used very much, just sitting there waiting, so I figured it's wiser to keep it closed and only open on demand. What do you think about that? I'm open for suggestions and critique. | |
Aug 14, 2014 at 19:48 | comment | added | Malachi | the Command implements the IDisposable interface like a Connection object. but you don't construct the connection object inside the private method, so I assume it is created and opened before you call this private method. no need to open a connection twice, and you should close the connection after the call to the private method. does that make sense, @KonradViltersten? | |
Aug 14, 2014 at 19:36 | comment | added | Konrad Viltersten | @Malachi I don't think it's going to work. One needs to open the connection first, right? Also, if I don't close the connection explicitly, will exiting the using's scope do that for me? Please note that your assumption on the connection being GCed elsewhere is correct (great mind think alike, right?) and it's only opening/closing I'm concerned about here. | |
Aug 14, 2014 at 16:21 | comment | added | Malachi | @svick I see what you are saying. you are right, the command is opening the connection, and it is separate still. | |
Aug 14, 2014 at 16:09 | comment | added | svick | “the connection is created by the Command object” No, it's not, it's created by some external code. You could claim that passing it into the constructor transfers ownership of the connection (streams often do something like that), but I guess that doesn't make sense here, since you can use a single connection for multiple commands. | |
Aug 14, 2014 at 15:57 | comment | added | Malachi | @svick, in that link for the disposing of the connection, the connection wasn't called using the connection method of the command either. I would think that this would change things a little bit because in OP's(Here) code the connection is created by the Command object and not a connection object, so disposing of the Command object should in theory dispose everything created by it. | |
Aug 14, 2014 at 15:54 | history | edited | Malachi | CC BY-SA 3.0 |
added 215 characters in body
|
Aug 14, 2014 at 15:49 | comment | added | Malachi |
@svick, the connection isn't declared inside of this method, I assume that the connection Connection is taken care of outside of the method, so really the connection shouldn't even need to be opened here, the command should be able to be executed without the open command.
|
|
Aug 14, 2014 at 15:31 | comment | added | svick |
And ExecuteScalar() returns object , so the cast is necessary.
|
|
Aug 14, 2014 at 15:29 | comment | added | svick |
Disposing SqlCommand won't dispose the connection, so this code isn't the same as the original.
|
|
Aug 14, 2014 at 14:15 | history | answered | Malachi | CC BY-SA 3.0 |