{"id":102,"date":"2016-02-17T13:23:04","date_gmt":"2016-02-17T13:23:04","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=102"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"insert-queries","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/databases\/sqlite\/queries\/insert-queries","title":{"rendered":"INSERT Queries"},"content":{"rendered":"<h4>\n\tINSERT Example<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tCommand1.CommandText = @&quot;INSERT INTO myTable (\r\n\t\t\t\t\t\tSomeValueColumn,\r\n\t\t\t\t\t\tSomeStringColumn\r\n\t\t\t\t\t) Values (\r\n\t\t\t\t\t\t@SomeValue,\r\n\t\t\t\t\t\t@SomeString\r\n\t\t\t\t\t)&quot;;\r\n\t\/\/Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.\r\n\t\/\/Other values can be added as parameters too or inline in the INSERT text.\r\n\tCommand1.Parameters.AddWithValue(&quot;@SomeValue&quot;, 1234);\r\n\tCommand1.Parameters.AddWithValue(&quot;@SomeString&quot;, &quot;Hello&quot;);\r\n\r\n\tCommand1.ExecuteNonQuery();\r\n<\/code><\/pre>\n<h4>\n\tINSERT and get Auto Increment value<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tint NewRowIdValue;\r\n\tCommand1.CommandText = @&quot;INSERT INTO myTable (\r\n\t\t\t\t\t\tSomeValueColumn,\r\n\t\t\t\t\t\tSomeStringColumn\r\n\t\t\t\t\t) Values (\r\n\t\t\t\t\t\t@SomeValue,\r\n\t\t\t\t\t\t@SomeString\r\n\t\t\t\t\t); SELECT last_insert_rowid() AS rowid FROM myTable LIMIT 1&quot;;\r\n\tCommand1.Parameters.AddWithValue(&quot;@SomeValue&quot;, 1234);\r\n\tCommand1.Parameters.AddWithValue(&quot;@SomeString&quot;, &quot;Hello&quot;);\r\n\r\n\t\/\/Command1.ExecuteNonQuery();\r\n\tSystem.Data.SQLite.SQLiteDataReader Reader1 = Command1.ExecuteReader();\t\t\/\/Get the auto index value\r\n\t{\r\n\t\tif (Reader1.Read())\r\n\t\t\tNewRowIdValue = Convert.ToInt32(Reader1[\"rowid\"]);\r\n\t}\r\n\tReader1.Close();\r\n<\/code><\/pre>\n<h4>\n\tInsert If Not Exists<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tCommand1-&gt;CommandText = &quot;INSERT INTO tblMyTable (\t\\\r\n\t\t\t\t\t\t\t\tFieldName,\t\t\t\t\t\\\r\n\t\t\t\t\t\t\t\tFieldIsActive\t\t\t\t\\\r\n\t\t\t\t\t\t\t) SELECT \t\t\t\t\t\t\\\r\n\t\t\t\t\t\t\t\t@FieldName,\t\t\t\t\t\\\r\n\t\t\t\t\t\t\t\t@FieldIsActive\t\t\t\t\\\r\n\t\t\t\t\t\t\tWHERE NOT EXISTS(SELECT 1 FROM tblMyTable WHERE FieldName = &#39;Part&#39;)&quot;;\r\n<\/code><\/pre>\n<h4>\n\tInserting Null<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tCommand1.Parameters.AddWithValue(&quot;@LanguageNameImage&quot;, DBNull.Value);\r\n<\/code><\/pre>\n<p>\n\tGet Auto Increment Row Value\n<\/p>\n<p>\n\tAfter doing the INSERT command do this:\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>INSERT Example Command1.CommandText = @&quot;INSERT INTO myTable ( SomeValueColumn, SomeStringColumn ) Values ( @SomeValue, @SomeString )&quot;; \/\/Strings should be added as parameters to avoid sanatisation risks unless you know they are safe. \/\/Other values can be added as parameters too or inline in the INSERT text. Command1.Parameters.AddWithValue(&quot;@SomeValue&quot;, 1234); Command1.Parameters.AddWithValue(&quot;@SomeString&quot;, &quot;Hello&quot;); Command1.ExecuteNonQuery(); INSERT and get Auto [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-queries"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/102\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}