Johnathon64
Johnathon64

Reputation: 1320

Cannot insert to database through console app

I am trying to write an insert method in a C# console application. I have set a breakpoint and it does hit ExecuteNonQuery, however, after stepping through it just hangs there and control does not seem to return back to the application. When I perform a read action on the database it works perfectly.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    SqlCommand command = new SqlCommand("INSERT INTO dbo.dbDevices (ipAddress) VALUES (@value)", connection);
    command.Parameters.Add("@value", "SUCCESS");
    command.ExecuteNonQuery();
}

I have also checked the active connections to the database and it says that it is awaiting command.

.Net SqlClient Data Provider     5720       AWAITING COMMAND

More information can be provided if needed.

Upvotes: 4

Views: 813

Answers (1)

msmolcic
msmolcic

Reputation: 6567

Not sure if it's related but you're passing parameter value using deprecated method. I recommend using AddWithValue method. So it's either:

command.Parameters.AddWithValue("@value", "SUCCESS");

or

command.Parameters.Add(new SqlParameter("@value", "SUCCESS"));

Upvotes: 1

Related Questions