sqlcommand

Is SqlCommand.Dispose enough?

Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } My concern is : Will the Dispose method of the SqlCommand (which is calle...

C#-SQL: How to execute a batch of StoredProcedure?

Edit: My problem is not a problem anymore: I have redo my performances tests and I have do a fatal stupid error: I had forget a x1000 to get seconds from milliseconds :/ Sorry for that guys. For info: - I do some 1900 updates per second from my PC to the DataBase server on local network. - 3.200 updates per second if the programs ...

Does .net SqlCommand.ExecuteReader close connection?

In this sentence: myCommand.ExecuteReader(CommandBehavior.CloseConnection) does it close connection in case of exception? ...

What's the best method to pass parameters to SQLCommand?

What's the best method to pass parameters to SQLCommand? You can do: cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob"; or cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob"; or cmd.Parameters.Add("@Name").Value = "Bob"; It seems like the first one might be somehow "better" either performance-wise or err...

How to change programmatically DataSet SqlCommand CommandText

Hello, I have a generated DataSet with bunch of tables. In one table I have a custom query that I use to select people that are older than 15 years. I would like to change this query during working program, so that the query will look like 'select * from people where age > 17'. Now it looks like 'select * from people where age > 15'. A...

How to obtain the IDbCommand output to the DB? (.NET)

I have a SqlCommand object, and I execute its ExecuteNonQuery method. The query fails, but I can't see how to obtain the query to debug it. Could you help me? Update I am currently using Wireshark (tcpdump) to read the command, but I think that its not the best solution Update The CommandText property only provides the Stored Procedure...

Asynchronous SQLCommand and CCR

I have been playing with the demo code from this msdn article by Jeffrey Richter. I have added a new function to his ApmToCcrAdapters to handle the SqlCommand.BeginExecuteReader. Only it is closing the reader before I can read it. The following code is used to provide a FromIteratorHandler: private static IEnumerator<ITask> Asy...

SQLCommand.ExecuteScalar() - why it throws a System.NullReferenceException?

Hi, Could anyone notice what could be wrong with the following function: public string Login(string username, string password) { string result = ""; string select = "SELECT user_id FROM [user] WHERE username = @username AND password = @password"; SqlConnection conn = new SqlConnection(connectionString); ...

How do I provide a string of Ids in a Select statement's IN clause even though the column is an integer

I'm paging data using an ObjectDataSource and I have the following method: public int GetNumberOfArticles(string employeeIds) { System.Data.DataTable dataTable; System.Data.SqlClient.SqlDataAdapter dataAdapter; System.Data.SqlClient.SqlCommand command; int numberOfArticles = 0; command = new System.Data.SqlClient.S...

How should I pass a user-defined type to SqlParameterCollection.AddWithValue?

I have a custom data type called StudentID, which has an implicit conversion to string. When I pass a StudentID instance to SqlCommand.Parameters.AddWithValue (as the value) and execute the command, I receive the following error: "No mapping exists from object type StudentID to a known managed provider native type." Specifying a type ...

Why is some sql query much slower when used with SqlCommand?

I have a stored procedure that executes much faster from Sql Server Management Studio (2 seconds) than when run with System.Data.SqlClient.SqlCommand (times out after 2 minutes). What could be the reason for this? Details: In Sql Server Management Studio this runs in 2 seconds (on production database): EXEC sp_Stat @DepartmentID...

What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET? ...

Refactor code into using statement

I have a dal layer with lots of methods, all of them call stored procedures, some return lists (so with a use of SqlDataReader), others only a specific value. I have a helper method that creates the SqlCommand: protected SqlCommand CreateSprocCommand(string name, bool includeReturn, SqlDbType returnType) { SqlConnectio...

How can I declare and use T-SQL variables across multiple SqlCommands using the same SqlConnection object to perform multiple inserts into a table variable?

I want to load a list of records given a possibly lengthy list of usernames (anywhere from one to thousands of usernames). Disregard how the name(s) are chosen, and assume they cannot be determined from any existing data in the database. This applies to SQL Server 2005. I specifically want to avoid using a single select statement with ...

SqlCommand.ExecuteScalar returns null but raw SQL does not

I have the following code that uses the SqlClient.ExecuteScalar method to return an ID from a table. using (var conn = new SqlConnection(connectionString)) using (var cmdContrib = new SqlCommand("SELECT ContributorId FROM Contributor WHERE Code='" + folderSystem.ContributorCode + "'", conn)) { conn.Open(); var contribId = cmdC...

Handling multiple calls to BeginExecuteNonQuery in SQL Server 2008

Hi all, I have an application that is receiving a high volume of data that I want to store in a database. My current strategy is to fire off an asynchronous call (BeginExecuteNonQuery) with each record when it's ready. I'm using the asynchronous call to ensure that the rest of the application runs smoothly. The problem I have is that...

SqlCommand C# Issue

The below piece of code that throws the following exception.. Error Message: Object reference not set to an instance of an object. Stack Trace: at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQ...

Executing a SQLCommand Without Specifiying a Transaction

Hi, I feel like I should be able to find the answer to this question but for the life of me I can't seem to get the information I'm looking for. We have some lists of data being fetched in our application via a SqlCommand performing a SELECT query on a SqlServer database. We do not explicitly setup a transaction on the SqlCommand, ins...

Does SqlDbType.DateTime not store seconds?

The environment I am using is C# 3.5, ado.net, sqlserver 2005. using System.Data; using System.Data.SqlClient; I am using a SqlCommand cmd.Parameters of SqlDbType.DateTime to create a record in the database. Seconds are not being stored in the database. The field in the db is of type datetime. Do I need to do something special to ...

How to avoid accidentally closing an SQL Connection in Java?

Currently what i am doing for transaction management is: Connection connection = getConnection(); connection.setAutoCommit(false); updateTableX ( connection, ... ); updateTableY ( connection, ... ); connection.commit(); closeConnection(); I would like to know, if it is possible to avoid closing the connection in my 'updateTableX' meth...