Questions tagged [sqlcommand]

Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database which is used in Microsoft .NET.

SqlCommand is a class in the .NET Framework whose instances represent a SQL statement to later be executed against a SQL Server database.

General syntax:

SqlCommand is instantiated with a query string and a connection. In C#:

SqlCommand cmd = new SqlCommand("select CategoryName from Categories", con);

Reference

SqlCommand on MSDN

919 questions
3
votes
2 answers

Why insert statement is slow with SqlCommand?

I am using SqlCommand to insert multiple records to the database but it takes long time to insert 2000 records, I did the following code: using (SqlConnection sql = new SqlConnection(connectionString)) { using (SqlCommand cmd = new…
Andres Camacho
  • 367
  • 4
  • 17
3
votes
2 answers

Get an error message from TSQL output clause with SqlDataReader

I have the following SQL INSERT INTO [dbo].[table1] ([val1], [val2]) OUTPUT INSERTED.* VALUES (@val1, @val2) I use SqlCommand.ExecuteReader() to get a SqlDataReader that reads the outputed row. It all works fine when the parameters are correct.…
m0sa
  • 10,712
  • 4
  • 44
  • 91
3
votes
2 answers

how to see the SQL commands in App Insights executed by QA environment of API app?

We developed API Application and published it into Azure using DevOps with two environments like Dev and QA. In Dev environment we are able to see SQL queries executed by APIs, but in QA environment we are unable to see the SQL queries executed by…
Pradeep
  • 5,101
  • 14
  • 68
  • 140
3
votes
2 answers

How to override SqlCommand to grab generated TSQL

Is there any way to intercept the SQL that's generated by SqlCommand? I currently have a method that will execute a stored procedure: public int ExecSP(string spName, params objec[] params) { using (SqlConnection con = new…
ODotN
  • 145
  • 7
3
votes
2 answers

While more query executing, i got "Not allowed to change the 'ConnectionString' property."

class CommonConnection { public class dStructure { public static string ConnectionString = ""; } public SqlConnection Conn; #region "Connection Procedures" public string ConnectionString { get { …
Nishit Jani
  • 40
  • 1
  • 7
3
votes
4 answers

SQL Invalid Column name detection from Query

I've tried running the code and I have no idea what's wrong with the query. Because it keeps saying invalid column name, when I'm trying to retrieve the data from that column instead. The column name matches the one in the DB. It's well connected…
Minial
  • 321
  • 2
  • 17
3
votes
2 answers

C# running temporary stored procedure

I have a SQL statement that I need to run in C# and would need to get parameters from C# code. I know stored procedures are preferred to avoid SQL injection but I am just looking to do this in C#. I am translating this SQL to C# but I encountered an…
Jan Navarro
  • 327
  • 4
  • 16
3
votes
1 answer

Why does the sqlParameter request a sourceColumn?

I was examining a sqlParameter example from Microsoft and am trying to understand: What are the reasons and benefits for specifying a SourceColumn? The sql command already specifies the target column. command = New SqlCommand( _ "INSERT…
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
3
votes
1 answer

Is there any negative effect to setting SqlCommand's CommandTimeout to a high value?

It seems that I was running into some random issues with a Stored Procedure when it took longer than the default timeout value; I was advised of that here. So I increased the value, and it seems to have helped. But it makes me wonder: Why not just…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
3
votes
4 answers

Which pattern is better for SqlConnection object?

Which pattern is better for SqlConnection object? Which is better in performance? Do you offer any other pattern? class DataAccess1 : IDisposable { private SqlConnection connection; public DataAccess1(string connectionString) { …
Jalal
  • 6,594
  • 9
  • 63
  • 100
3
votes
2 answers

How to "reset" C# SqlCommand object so I can re-use it in a loop

I have this code in a foreach loop that calls a sql function for each folder foreach (string strCurrentFolder in strLocalSubFolderList) { SqlCommand sqlComm1 = new SqlCommand("dbo.fnChkXfer", _sqlConn); sqlComm1.CommandType =…
Maa421s
  • 159
  • 1
  • 12
3
votes
2 answers

SqlCommand cannot drop triggers

I am creating test methods in .Net that needs to alter a database for setting testing scenarios. We use SqlCommands to send queries to alter a database, the connection strings for this are trusted connection strings. All DROPS and ALTER queries…
Mauricio Quintana
  • 391
  • 1
  • 4
  • 13
3
votes
1 answer

Select most frequent from column in

Hi i have a columm in the table and i want to select the most common item from the selected column. The table is set up publication: id Title published I want to beable to select the most recurring places where publications have been published.…
Dean
  • 8,668
  • 17
  • 57
  • 86
3
votes
3 answers

Do i need to dispose of MySqlCommand?

I find it incredibly annoying to write a using statement on every one of my queries (which require its own command or write parameters.clear()) which sometimes require declaring variables outside of the using block. Its so incredibly annoying and…
user34537
3
votes
2 answers

SQL Check if table Exists in C#, if not create

I think I've seen almost every page relating to this question, most likely answer was Check if a SQL table exists but didn't really understand it. This is what I got: private void select_btn_Click(object sender, EventArgs e) { string…
CularBytes
  • 9,924
  • 8
  • 76
  • 101