{"id":2662,"date":"2018-03-22T15:48:39","date_gmt":"2018-03-22T14:48:39","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=2662"},"modified":"2018-03-22T15:48:39","modified_gmt":"2018-03-22T14:48:39","slug":"c-net-ms-sql-server-nach-insert-direkt-die-auto-increment-id-erhalten-ohne-zweite-abfrage","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=2662","title":{"rendered":"C#.NET + MS SQL Server : Nach INSERT direkt die Auto-Increment ID erhalten ohne zweite Abfrage"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Um die Auto-Increment ID zu erhalten werden des\u00f6fteren 2 Statements abgesetzt, obwohl das INSERT-Statement direkt die Auto-Increment ID zur\u00fcckgeben kann<\/p>\n<h1>Ansatz &#8211; Approach<\/h1>\n<p>Anstelle von comm.ExecuteNonQuery() sollte man lieber comm.ExecuteScalar() mit SELECT SCOPE_IDENTITY() kombinieren.<\/p>\n<h1>L\u00f6sung &#8211; Solution <\/h1>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic string insertMainTherapyData(string serial_no\r\n                    ,string therapy_start\r\n                    ,string machine_type\r\n                    ,string file_source\r\n                    ,string dialog_version\r\n                    ,string tlc_version\r\n                    ,string versions\r\n                    ,DateTime uploaded_on\r\n                    ,string uploaded_by\r\n                    ,bool processed\r\n                    ,string user_original_file\r\n                    ,string user_country\r\n                    ,string user_comment\r\n                    ,string user_upload_reason\r\n                    ,string user_location)\r\n{\r\n    string sqlStatement=@&quot;\r\n        INSERT INTO &#x5B;dbo].&#x5B;therapy]\r\n                    (&#x5B;serial_no]\r\n                    ,&#x5B;therapy_start]\r\n                    ,&#x5B;machine_type]\r\n                    ,&#x5B;file_source]\r\n                    ,&#x5B;dialog_version]\r\n                    ,&#x5B;tlc_version]\r\n                    ,&#x5B;versions]\r\n                    ,&#x5B;uploaded_on]\r\n                    ,&#x5B;uploaded_by]\r\n                    ,&#x5B;processed]\r\n                    ,&#x5B;user_original_file]\r\n                    ,&#x5B;user_country]\r\n                    ,&#x5B;user_comment]\r\n                    ,&#x5B;user_upload_reason]\r\n                    ,&#x5B;user_location])\r\n        VALUES\r\n                    (@serial_no\r\n                    ,@therapy_start\r\n                    ,@machine_type\r\n                    ,@file_source\r\n                    ,@dialog_version\r\n                    ,@tlc_version\r\n                    ,@versions\r\n                    ,@uploaded_on\r\n                    ,@uploaded_by\r\n                    ,@processed\r\n                    ,@user_original_file\r\n                    ,@user_country\r\n                    ,@user_comment\r\n                    ,@user_upload_reason\r\n                    ,@user_location);\r\n            SELECT SCOPE_IDENTITY()\r\n            &quot;;\r\n    int myID = -1;\r\n    SqlConnection conn = new SqlConnection(MyConfigurationManager.prdSqlServerString);\r\n    try\r\n    {\r\n        conn.Open();\r\n        SqlCommand comm = new SqlCommand();\r\n        comm.Connection = conn;\r\n        comm.CommandText = sqlStatement;\r\n        comm.Parameters.AddWithValue(&quot;serial_no&quot;, serial_no);\r\n        comm.Parameters.AddWithValue(&quot;therapy_start&quot;, therapy_start);\r\n        comm.Parameters.AddWithValue(&quot;machine_type&quot;, machine_type);\r\n        comm.Parameters.AddWithValue(&quot;file_source&quot;, file_source);\r\n        comm.Parameters.AddWithValue(&quot;dialog_version&quot;, dialog_version);\r\n        comm.Parameters.AddWithValue(&quot;tlc_version&quot;, tlc_version);\r\n        comm.Parameters.AddWithValue(&quot;versions&quot;, versions);\r\n        comm.Parameters.AddWithValue(&quot;uploaded_on&quot;, uploaded_on);\r\n        comm.Parameters.AddWithValue(&quot;uploaded_by&quot;, uploaded_by);\r\n        comm.Parameters.AddWithValue(&quot;processed&quot;, processed);\r\n        comm.Parameters.AddWithValue(&quot;user_original_file&quot;, user_original_file);\r\n        comm.Parameters.AddWithValue(&quot;user_country&quot;, user_country);\r\n        comm.Parameters.AddWithValue(&quot;user_comment&quot;, user_comment);\r\n        comm.Parameters.AddWithValue(&quot;user_upload_reason&quot;, user_upload_reason);\r\n        comm.Parameters.AddWithValue(&quot;user_location&quot;, user_location);\r\n        myID = Convert.ToInt32(comm.ExecuteScalar());\r\n    }\r\n    catch (Exception ex)\r\n    {\r\n        return &quot;ERROR: &quot;+ex.Message;\r\n    }\r\n    finally\r\n    {\r\n        conn.Close();\r\n    }\r\n\r\n    return myID.ToString();\r\n}\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D2662&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem Um die Auto-Increment ID zu erhalten werden des\u00f6fteren 2 Statements abgesetzt, obwohl das INSERT-Statement direkt die Auto-Increment ID zur\u00fcckgeben kann Ansatz &#8211; Approach Anstelle von comm.ExecuteNonQuery() sollte man lieber comm.ExecuteScalar() mit SELECT SCOPE_IDENTITY() kombinieren. L\u00f6sung &#8211; Solution public string insertMainTherapyData(string serial_no ,string therapy_start ,string machine_type ,string file_source ,string dialog_version ,string tlc_version ,string versions ,DateTime &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=2662\" class=\"more-link\"><span class=\"screen-reader-text\">C#.NET + MS SQL Server : Nach INSERT direkt die Auto-Increment ID erhalten ohne zweite Abfrage<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,33,11,3],"tags":[],"class_list":["post-2662","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-ms-sql-server","category-programmierung"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-GW","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2662"}],"version-history":[{"count":1,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2662\/revisions"}],"predecessor-version":[{"id":2663,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2662\/revisions\/2663"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}