Conexión a PostgreSQL mediante ADO.NET
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Ejecuta una instrucción de SQL INSERT para abrir y cerrar una conexión con Cloud SQL para PostgreSQL mediante el paquete System.Data.Common de ADO.NET.
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]