2012-12-26 生のADO.NETのコードサンプル遅いのがまずいなら、早くすれば良いのではないか。連続して今日の分の記録。 生の ADO.NET での INSERT 文のサンプル。元ページのコメントも参照。 var providerName = "Npgsql"; var connectionString = "Server=db.bouzuya.jp;Port=5432;User Id=postgres;Password=postgres;Database=mydb"; var sql = "INSERT INTO users(username,password) VALUES(:username, :password)"; var parameters = new Dictionary<string, object>() { {"username", "bouzuya"}, {"password", "password"} }; var factory = DbProviderFactories.GetFactory(providerName); using (var connection = factory.CreateConnection ()) { connection.ConnectionString = connectionString; connection.Open (); using (var transaction = connection.BeginTransaction ()) { try { int result; using (var command = connection.CreateCommand ()) { command.Connection = connection; command.Transaction = transaction; command.CommandType = CommandType.Text; command.CommandText = sql; foreach (var parameter in parameters) { var p = command.CreateParameter(); p.ParameterName = parameter.Key; p.Value = parameter.Value; command.Parameters.Add (p); } result = command.ExecuteNonQuery (); } transaction.Commit (); return result; } catch { transaction.Rollback (); throw; } } } 10 min.