65450

writing to sql server from C#, no errors returned

Question:

I am trying to write to a database from c#:

using (SqlConnection connection = new SqlConnection()) { try { connection.ConnectionString = "Data Source=nesoi;Initial Catalog=SalesDWH;Integrated Security=True"; // This creates an object with which you can execute sql using (SqlCommand command = connection.CreateCommand()) { command.CommandText = @"INSERT INTO [SalesDWH].[dbo].[PendingSpecimens] ([Date Entered] ,[Specimen ID] ,[Test] ,[Agency]) VALUES (@DateEntered,@SpecimenID,@Test,@Agency)"; command.CommandType = CommandType.Text; // This is how you add a parameter to your sql command // This way you are protected against SQL injection attacks SqlParameter DateEntered = command.CreateParameter(); DateEntered.ParameterName = "@DateEntered"; DateEntered.Value = fields[0]; command.Parameters.Add(DateEntered); SqlParameter SpecimenID = command.CreateParameter(); SpecimenID.ParameterName = "@SpecimenID"; SpecimenID.Value = fields[1]; command.Parameters.Add(SpecimenID); SqlParameter Test = command.CreateParameter(); Test.ParameterName = "@Test"; Test.Value = fields[2]; command.Parameters.Add(Test); SqlParameter Agency = command.CreateParameter(); Agency.ParameterName = "@Agency"; Agency.Value = fields[4]; command.Parameters.Add(Agency); connection.Open(); int someint=command.ExecuteNonQuery(); } } catch(Exception ee) { textBox1.Text = ee.ToString(); }

In addition to no errors being returned, it has not written anything either!

What am I doing wrong?

I suspect that this line:

command.ExecuteNonQuery();

is not working.

Mut I do not understand why

please help!

Answer1:

Try this:

using (SqlConnection connection = new SqlConnection("Data Source=nesoi;Initial Catalog=SalesDWH;Integrated Security=True")) { string queryString = "INSERT INTO SalesDWH.dbo.PendingSpecimens([Date Entered], [Specimen ID], Test, Agency) VALUES (" + fields[0] + ", " + fields[1] + ", " + fields[2] + ", " + fields[4] + ")"; SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); }

Answer2:

Maybe you are missing the parentheses in the values clause?

Recommend

  • Returning raw query as array yii 2
  • how to insert DateTime.Now values into datetime type field in sql database
  • In yii how to get success message after executing sql
  • XSL-T Adding Missing schema elements from input xml as empty tags
  • Accessing Sql FILESTREAM from within a CLR stored procedure
  • WCF Service Application returns 404 using WebGet
  • Determining if a string has been entered with a leading single quote
  • Type mismatch: cannot convert from Connection to Connection
  • Drag and Drop for imageview not working
  • How do you SELECT several columns with one distinct column
  • Can't see some objects' internals in Qt Creator's Locals and Expressions debug window
  • How to retrieve multiple columns from non-entity type sql query?
  • Recreate the Oracle DUAL table
  • Insert records if not exist SQL Server 2005
  • how to query for min or max inet/cidr with postgres
  • d3.js selection conditional rendering
  • Use default value of a column in stored procedures
  • INSERT EXEC Statement cannot be nested [duplicate]
  • Returning the auto incrementing value after an insert using slick
  • How can Delete be both a DDL and a DML statement
  • Enabling DTD support in Sql Server
  • SAXReader not re-ecape characters
  • Adding elements to a huge XML file
  • close() was never explicitly called on database
  • Eliminate partial duplicate rows from result set
  • Transactional Create with Validation in ServiceStack Redis Client
  • Row Count Is Returning the incorrect number using RaptureXML
  • Illegal mix of collations for operation for date/time comparison
  • Jenkins: How To Build multiple projects from a TFS repository?
  • PHP - How to update data to MySQL when click a radio button
  • How do I fake an specific browser client when using Java's Net library?
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Release, debug version and Authorization Google?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query