ADO.NET による PostgreSQL 接続の管理
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
SQL INSERT ステートメントを実行し、ADO.NET System.Data.Common パッケージを使用して Cloud SQL for PostgreSQL との接続を管理します。
コードサンプル
C#
Cloud SQL for PostgreSQL に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],[],[],[],null,["# PostgreSQL ADO.NET connection\n\nRun a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using the ADO.NET System.Data.Common package.\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud SQL for PostgreSQL, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n insertTimestamp = DateTime.UtcNow;\n try\n {\n using(var connection = new NpgsqlConnection(_connectionString.ConnectionString))\n { \n connection.OpenWithRetry();\n using (var insertVoteCommand = connection.CreateCommand())\n {\n insertVoteCommand.CommandText =\n @\"INSERT INTO votes (candidate, time_cast) VALUES (@candidate, @time_cast)\";\n var candidate = insertVoteCommand.CreateParameter();\n candidate.ParameterName = \"@candidate\";\n candidate.DbType = DbType.String;\n candidate.Value = team;\n insertVoteCommand.Parameters.Add(candidate);\n var timeCast = insertVoteCommand.CreateParameter();\n timeCast.ParameterName = \"@time_cast\";\n timeCast.DbType = DbType.DateTime;\n timeCast.Value = insertTimestamp;\n insertVoteCommand.Parameters.Add(timeCast);\n await insertVoteCommand.ExecuteNonQueryAsync();\n }\n }\n return Content($\"Vote successfully cast for '{team}' at time {insertTimestamp}!\");\n }\n catch (Exception ex)\n {\n // If something goes wrong, handle the error in this\n // section. This might involve retrying or adjusting\n // parameters depending on the situation.\n return StatusCode((int)HttpStatusCode.InternalServerError, ex);\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloud_sql_postgres)."]]